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 <sys/queue.h>
18 #include <sys/types.h>
35 #include "got_version.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_cancel.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_repository_dump.h"
43 #include "got_repository_load.h"
44 #include "got_gotconfig.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
48 #include "got_worktree.h"
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
58 catch_sigint(int signo)
64 catch_sigpipe(int signo)
69 static const struct got_error *
70 check_cancelled(void *arg)
72 if (sigint_received || sigpipe_received)
73 return got_error(GOT_ERR_CANCELLED);
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
84 __dead static void usage(int, int);
85 __dead static void usage_init(void);
86 __dead static void usage_info(void);
87 __dead static void usage_pack(void);
88 __dead static void usage_indexpack(void);
89 __dead static void usage_listpack(void);
90 __dead static void usage_cleanup(void);
91 __dead static void usage_dump(void);
92 __dead static void usage_load(void);
94 static const struct got_error* cmd_init(int, char *[]);
95 static const struct got_error* cmd_info(int, char *[]);
96 static const struct got_error* cmd_pack(int, char *[]);
97 static const struct got_error* cmd_indexpack(int, char *[]);
98 static const struct got_error* cmd_listpack(int, char *[]);
99 static const struct got_error* cmd_cleanup(int, char *[]);
100 static const struct got_error* cmd_dump(int, char *[]);
101 static const struct got_error* cmd_load(int, char *[]);
103 static const struct gotadmin_cmd gotadmin_commands[] = {
104 { "init", cmd_init, usage_init, "" },
105 { "info", cmd_info, usage_info, "" },
106 { "pack", cmd_pack, usage_pack, "" },
107 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
108 { "listpack", cmd_listpack, usage_listpack, "ls" },
109 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
110 { "dump", cmd_dump, usage_dump, "" },
111 { "load", cmd_load, usage_load, "" },
115 list_commands(FILE *fp)
119 fprintf(fp, "commands:");
120 for (i = 0; i < nitems(gotadmin_commands); i++) {
121 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
122 fprintf(fp, " %s", cmd->cmd_name);
128 main(int argc, char *argv[])
130 const struct gotadmin_cmd *cmd;
133 int hflag = 0, Vflag = 0;
134 static const struct option longopts[] = {
135 { "version", no_argument, NULL, 'V' },
139 setlocale(LC_CTYPE, "");
141 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
161 got_version_print_str();
166 usage(hflag, hflag ? 0 : 1);
168 signal(SIGINT, catch_sigint);
169 signal(SIGPIPE, catch_sigpipe);
171 for (i = 0; i < nitems(gotadmin_commands); i++) {
172 const struct got_error *error;
174 cmd = &gotadmin_commands[i];
176 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
177 strcmp(cmd->cmd_alias, argv[0]) != 0)
183 error = cmd->cmd_main(argc, argv);
184 if (error && error->code != GOT_ERR_CANCELLED &&
185 error->code != GOT_ERR_PRIVSEP_EXIT &&
186 !(sigpipe_received &&
187 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
189 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
190 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
197 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
198 list_commands(stderr);
203 usage(int hflag, int status)
205 FILE *fp = (status == 0) ? stdout : stderr;
207 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
214 static const struct got_error *
215 apply_unveil(const char *repo_path, int repo_read_only)
217 const struct got_error *err;
220 if (unveil("gmon.out", "rwc") != 0)
221 return got_error_from_errno2("unveil", "gmon.out");
223 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
224 return got_error_from_errno2("unveil", repo_path);
226 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
227 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
229 err = got_privsep_unveil_exec_helpers();
233 if (unveil(NULL, NULL) != 0)
234 return got_error_from_errno("unveil");
242 fprintf(stderr, "usage: %s info [-r repository-path]\n",
247 static const struct got_error *
248 get_repo_path(char **repo_path)
250 const struct got_error *err = NULL;
251 struct got_worktree *worktree = NULL;
256 cwd = getcwd(NULL, 0);
258 return got_error_from_errno("getcwd");
260 err = got_worktree_open(&worktree, cwd, NULL);
262 if (err->code != GOT_ERR_NOT_WORKTREE)
268 *repo_path = strdup(got_worktree_get_repo_path(worktree));
270 *repo_path = strdup(cwd);
271 if (*repo_path == NULL)
272 err = got_error_from_errno("strdup");
275 got_worktree_close(worktree);
283 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
288 static const struct got_error *
289 cmd_init(int argc, char *argv[])
291 const struct got_error *error = NULL;
292 const char *head_name = NULL;
293 char *repo_path = NULL;
297 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
301 while ((ch = getopt(argc, argv, "b:")) != -1) {
318 repo_path = strdup(argv[0]);
319 if (repo_path == NULL)
320 return got_error_from_errno("strdup");
322 got_path_strip_trailing_slashes(repo_path);
324 error = got_path_mkdir(repo_path);
326 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
329 error = apply_unveil(repo_path, 0);
333 error = got_repo_init(repo_path, head_name);
339 static const struct got_error *
340 cmd_info(int argc, char *argv[])
342 const struct got_error *error = NULL;
343 char *repo_path = NULL;
344 struct got_repository *repo = NULL;
345 const struct got_gotconfig *gotconfig = NULL;
346 int ch, npackfiles, npackedobj, nobj;
347 off_t packsize, loose_size;
348 char scaled[FMT_SCALED_STRSIZE];
349 int *pack_fds = NULL;
352 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
357 while ((ch = getopt(argc, argv, "r:")) != -1) {
360 repo_path = realpath(optarg, NULL);
361 if (repo_path == NULL)
362 return got_error_from_errno2("realpath",
364 got_path_strip_trailing_slashes(repo_path);
375 if (repo_path == NULL) {
376 error = get_repo_path(&repo_path);
380 error = got_repo_pack_fds_open(&pack_fds);
383 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
387 /* Remove "cpath" promise. */
388 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
392 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
396 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
398 gotconfig = got_repo_get_gotconfig(repo);
400 const struct got_remote_repo *remotes;
402 if (got_gotconfig_get_author(gotconfig)) {
403 printf("default author: %s\n",
404 got_gotconfig_get_author(gotconfig));
406 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
407 for (i = 0; i < nremotes; i++) {
408 const char *fetch_url = remotes[i].fetch_url;
409 const char *send_url = remotes[i].send_url;
410 if (strcmp(fetch_url, send_url) == 0) {
411 printf("remote \"%s\": %s\n", remotes[i].name,
412 remotes[i].fetch_url);
414 printf("remote \"%s\" (fetch): %s\n",
415 remotes[i].name, remotes[i].fetch_url);
416 printf("remote \"%s\" (send): %s\n",
417 remotes[i].name, remotes[i].send_url);
422 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
426 printf("pack files: %d\n", npackfiles);
427 if (npackfiles > 0) {
428 if (fmt_scaled(packsize, scaled) == -1) {
429 error = got_error_from_errno("fmt_scaled");
432 printf("packed objects: %d\n", npackedobj);
433 printf("packed total size: %s\n", scaled);
436 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
439 printf("loose objects: %d\n", nobj);
441 if (fmt_scaled(loose_size, scaled) == -1) {
442 error = got_error_from_errno("fmt_scaled");
445 printf("loose total size: %s\n", scaled);
449 got_repo_close(repo);
451 const struct got_error *pack_err =
452 got_repo_pack_fds_close(pack_fds);
464 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
465 "[-x reference] [reference ...]\n", getprogname());
469 struct got_pack_progress_arg {
471 char last_scaled_size[FMT_SCALED_STRSIZE];
483 int printed_something;
487 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
488 int ncolored, int nfound, int ntrees)
491 fprintf(out, "%d commit%s colored", ncolored,
492 ncolored == 1 ? "" : "s");
495 fprintf(out, "%s%d object%s found",
496 ncolored > 0 ? "; " : "",
497 nfound, nfound == 1 ? "" : "s");
500 fprintf(out, "; %d tree%s scanned", ntrees,
501 ntrees == 1 ? "" : "s");
505 static const struct got_error *
506 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
507 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
510 struct got_pack_progress_arg *a = arg;
511 char scaled_size[FMT_SCALED_STRSIZE];
512 int p_deltify, p_written;
513 int print_colored = 0, print_found = 0, print_trees = 0;
514 int print_searching = 0, print_total = 0;
515 int print_deltify = 0, print_written = 0;
517 if (a->verbosity < 0)
520 if (a->last_ncolored != ncolored) {
522 a->last_ncolored = ncolored;
525 if (a->last_nfound != nfound) {
528 a->last_nfound = nfound;
531 if (a->last_ntrees != ntrees) {
535 a->last_ntrees = ntrees;
538 if ((print_colored || print_found || print_trees) &&
540 fprintf(a->out, "\r");
541 print_load_info(a->out, print_colored, print_found,
542 print_trees, ncolored, nfound, ntrees);
543 a->printed_something = 1;
546 } else if (!a->loading_done) {
547 fprintf(a->out, "\r");
548 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
549 fprintf(a->out, "\n");
553 if (fmt_scaled(packfile_size, scaled_size) == -1)
554 return got_error_from_errno("fmt_scaled");
556 if (a->last_ncommits != ncommits) {
558 a->last_ncommits = ncommits;
561 if (a->last_nobj_total != nobj_total) {
564 a->last_nobj_total = nobj_total;
567 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
568 strcmp(scaled_size, a->last_scaled_size)) != 0) {
569 if (strlcpy(a->last_scaled_size, scaled_size,
570 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
571 return got_error(GOT_ERR_NO_SPACE);
574 if (nobj_deltify > 0 || nobj_written > 0) {
575 if (nobj_deltify > 0) {
576 p_deltify = (nobj_deltify * 100) / nobj_total;
577 if (p_deltify != a->last_p_deltify) {
578 a->last_p_deltify = p_deltify;
584 if (nobj_written > 0) {
585 p_written = (nobj_written * 100) / nobj_total;
586 if (p_written != a->last_p_written) {
587 a->last_p_written = p_written;
596 if (print_searching || print_total || print_deltify || print_written)
597 fprintf(a->out, "\r");
599 fprintf(a->out, "packing %d reference%s", ncommits,
600 ncommits == 1 ? "" : "s");
602 fprintf(a->out, "; %d object%s", nobj_total,
603 nobj_total == 1 ? "" : "s");
605 fprintf(a->out, "; deltify: %d%%", p_deltify);
607 fprintf(a->out, "; writing pack: %*s %d%%",
608 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
609 if (print_searching || print_total || print_deltify ||
611 a->printed_something = 1;
617 static const struct got_error *
618 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
619 int nobj_indexed, int nobj_loose, int nobj_resolved)
621 struct got_pack_progress_arg *a = arg;
622 char scaled_size[FMT_SCALED_STRSIZE];
623 int p_indexed, p_resolved;
624 int print_size = 0, print_indexed = 0, print_resolved = 0;
626 if (a->verbosity < 0)
629 if (packfile_size > 0 || nobj_indexed > 0) {
630 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
631 (a->last_scaled_size[0] == '\0' ||
632 strcmp(scaled_size, a->last_scaled_size)) != 0) {
634 if (strlcpy(a->last_scaled_size, scaled_size,
635 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
636 return got_error(GOT_ERR_NO_SPACE);
638 if (nobj_indexed > 0) {
639 p_indexed = (nobj_indexed * 100) / nobj_total;
640 if (p_indexed != a->last_p_indexed) {
641 a->last_p_indexed = p_indexed;
646 if (nobj_resolved > 0) {
647 p_resolved = (nobj_resolved * 100) /
648 (nobj_total - nobj_loose);
649 if (p_resolved != a->last_p_resolved) {
650 a->last_p_resolved = p_resolved;
658 if (print_size || print_indexed || print_resolved)
661 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
663 printf("; indexing %d%%", p_indexed);
665 printf("; resolving deltas %d%%", p_resolved);
666 if (print_size || print_indexed || print_resolved)
672 static const struct got_error *
673 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
674 const char *refname, struct got_repository *repo)
676 const struct got_error *err;
677 struct got_reference *ref;
681 err = got_ref_open(&ref, repo, refname, 0);
683 if (err->code != GOT_ERR_NOT_REF)
686 /* Treat argument as a reference prefix. */
687 err = got_ref_list(refs, repo, refname,
688 got_ref_cmp_by_name, NULL);
690 err = got_reflist_insert(new, refs, ref,
691 got_ref_cmp_by_name, NULL);
692 if (err || *new == NULL /* duplicate */)
699 static const struct got_error *
700 cmd_pack(int argc, char *argv[])
702 const struct got_error *error = NULL;
703 char *repo_path = NULL;
704 struct got_repository *repo = NULL;
705 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
706 struct got_object_id *pack_hash = NULL;
708 struct got_pack_progress_arg ppa;
709 FILE *packfile = NULL;
710 struct got_pathlist_head exclude_args;
711 struct got_pathlist_entry *pe;
712 struct got_reflist_head exclude_refs;
713 struct got_reflist_head include_refs;
714 struct got_reflist_entry *re, *new;
715 int *pack_fds = NULL;
717 TAILQ_INIT(&exclude_args);
718 TAILQ_INIT(&exclude_refs);
719 TAILQ_INIT(&include_refs);
722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
727 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
739 repo_path = realpath(optarg, NULL);
740 if (repo_path == NULL)
741 return got_error_from_errno2("realpath",
743 got_path_strip_trailing_slashes(repo_path);
746 got_path_strip_trailing_slashes(optarg);
747 error = got_pathlist_append(&exclude_args,
761 if (repo_path == NULL) {
762 error = get_repo_path(&repo_path);
766 error = got_repo_pack_fds_open(&pack_fds);
769 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
773 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
777 TAILQ_FOREACH(pe, &exclude_args, entry) {
778 const char *refname = pe->path;
779 error = add_ref(&new, &exclude_refs, refname, repo);
785 error = got_ref_list(&include_refs, repo, "",
786 got_ref_cmp_by_name, NULL);
790 for (i = 0; i < argc; i++) {
792 got_path_strip_trailing_slashes(argv[i]);
794 error = add_ref(&new, &include_refs, refname, repo);
800 /* Ignore references in the refs/got/ namespace. */
801 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
802 const char *refname = got_ref_get_name(re->ref);
803 if (strncmp("refs/got/", refname, 9) != 0)
805 TAILQ_REMOVE(&include_refs, re, entry);
806 got_ref_close(re->ref);
810 memset(&ppa, 0, sizeof(ppa));
812 ppa.last_scaled_size[0] = '\0';
813 ppa.last_p_indexed = -1;
814 ppa.last_p_resolved = -1;
815 ppa.verbosity = verbosity;
817 error = got_repo_pack_objects(&packfile, &pack_hash,
818 &include_refs, &exclude_refs, repo, loose_obj_only,
819 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
821 if (ppa.printed_something)
826 error = got_object_id_str(&id_str, pack_hash);
830 printf("\nWrote %s.pack\n", id_str);
832 error = got_repo_index_pack(packfile, pack_hash, repo,
833 pack_index_progress, &ppa, check_cancelled, NULL);
837 printf("\nIndexed %s.pack\n", id_str);
840 got_repo_close(repo);
842 const struct got_error *pack_err =
843 got_repo_pack_fds_close(pack_fds);
847 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
848 got_ref_list_free(&exclude_refs);
849 got_ref_list_free(&include_refs);
857 usage_indexpack(void)
859 fprintf(stderr, "usage: %s indexpack packfile-path\n",
864 static const struct got_error *
865 cmd_indexpack(int argc, char *argv[])
867 const struct got_error *error = NULL;
868 struct got_repository *repo = NULL;
870 struct got_object_id *pack_hash = NULL;
871 char *packfile_path = NULL;
873 struct got_pack_progress_arg ppa;
874 FILE *packfile = NULL;
875 int *pack_fds = NULL;
877 while ((ch = getopt(argc, argv, "")) != -1) {
891 packfile_path = realpath(argv[0], NULL);
892 if (packfile_path == NULL)
893 return got_error_from_errno2("realpath", argv[0]);
896 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
901 error = got_repo_pack_fds_open(&pack_fds);
904 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
908 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
912 memset(&ppa, 0, sizeof(ppa));
914 ppa.last_scaled_size[0] = '\0';
915 ppa.last_p_indexed = -1;
916 ppa.last_p_resolved = -1;
918 error = got_repo_find_pack(&packfile, &pack_hash, repo,
923 error = got_object_id_str(&id_str, pack_hash);
927 error = got_repo_index_pack(packfile, pack_hash, repo,
928 pack_index_progress, &ppa, check_cancelled, NULL);
931 printf("\nIndexed %s.pack\n", id_str);
934 got_repo_close(repo);
936 const struct got_error *pack_err =
937 got_repo_pack_fds_close(pack_fds);
949 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
954 struct gotadmin_list_pack_cb_args {
964 static const struct got_error *
965 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
966 off_t size, off_t base_offset, struct got_object_id *base_id)
968 const struct got_error *err;
969 struct gotadmin_list_pack_cb_args *a = arg;
970 char *id_str, *delta_str = NULL, *base_id_str = NULL;
971 const char *type_str;
973 err = got_object_id_str(&id_str, id);
978 case GOT_OBJ_TYPE_BLOB:
979 type_str = GOT_OBJ_LABEL_BLOB;
982 case GOT_OBJ_TYPE_TREE:
983 type_str = GOT_OBJ_LABEL_TREE;
986 case GOT_OBJ_TYPE_COMMIT:
987 type_str = GOT_OBJ_LABEL_COMMIT;
990 case GOT_OBJ_TYPE_TAG:
991 type_str = GOT_OBJ_LABEL_TAG;
994 case GOT_OBJ_TYPE_OFFSET_DELTA:
995 type_str = "offset-delta";
996 if (asprintf(&delta_str, " base-offset %lld",
997 (long long)base_offset) == -1) {
998 err = got_error_from_errno("asprintf");
1003 case GOT_OBJ_TYPE_REF_DELTA:
1004 type_str = "ref-delta";
1005 err = got_object_id_str(&base_id_str, base_id);
1008 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1009 err = got_error_from_errno("asprintf");
1015 err = got_error(GOT_ERR_OBJ_TYPE);
1018 if (a->human_readable) {
1019 char scaled[FMT_SCALED_STRSIZE];
1021 if (fmt_scaled(size, scaled) == -1) {
1022 err = got_error_from_errno("fmt_scaled");
1026 while (isspace((unsigned char)*s))
1028 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1029 (long long)offset, s, delta_str ? delta_str : "");
1031 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1032 (long long)offset, (long long)size,
1033 delta_str ? delta_str : "");
1042 static const struct got_error *
1043 cmd_listpack(int argc, char *argv[])
1045 const struct got_error *error = NULL;
1046 struct got_repository *repo = NULL;
1048 struct got_object_id *pack_hash = NULL;
1049 char *packfile_path = NULL;
1050 char *id_str = NULL;
1051 struct gotadmin_list_pack_cb_args lpa;
1052 FILE *packfile = NULL;
1053 int show_stats = 0, human_readable = 0;
1054 int *pack_fds = NULL;
1057 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1062 while ((ch = getopt(argc, argv, "hs")) != -1) {
1081 packfile_path = realpath(argv[0], NULL);
1082 if (packfile_path == NULL)
1083 return got_error_from_errno2("realpath", argv[0]);
1085 error = got_repo_pack_fds_open(&pack_fds);
1088 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1092 /* Remove "cpath" promise. */
1093 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1097 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1101 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1105 error = got_object_id_str(&id_str, pack_hash);
1109 memset(&lpa, 0, sizeof(lpa));
1110 lpa.human_readable = human_readable;
1111 error = got_repo_list_pack(packfile, pack_hash, repo,
1112 list_pack_cb, &lpa, check_cancelled, NULL);
1116 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1117 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1118 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1119 lpa.noffdeltas + lpa.nrefdeltas,
1120 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1121 lpa.noffdeltas, lpa.nrefdeltas);
1125 got_repo_close(repo);
1127 const struct got_error *pack_err =
1128 got_repo_pack_fds_close(pack_fds);
1134 free(packfile_path);
1141 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1146 struct got_cleanup_progress_arg {
1150 int last_nredundant;
1152 int printed_something;
1156 static const struct got_error *
1157 cleanup_progress(void *arg, int ncommits, int nloose, int npurged,
1160 struct got_cleanup_progress_arg *a = arg;
1161 int print_loose = 0, print_commits = 0, print_purged = 0;
1162 int print_redundant = 0;
1164 if (a->last_ncommits != ncommits) {
1166 a->last_ncommits = ncommits;
1168 if (a->last_nloose != nloose) {
1171 a->last_nloose = nloose;
1173 if (a->last_npurged != npurged) {
1177 a->last_npurged = npurged;
1179 if (a->last_nredundant != nredundant) {
1183 print_redundant = 1;
1184 a->last_nredundant = nredundant;
1187 if (a->verbosity < 0)
1190 if (print_loose || print_commits || print_purged || print_redundant)
1193 printf("%d commit%s scanned", ncommits,
1194 ncommits == 1 ? "" : "s");
1196 printf("; %d loose object%s", nloose, nloose == 1 ? "" : "s");
1197 if (print_purged || print_redundant) {
1199 printf("; could purge %d object%s", npurged,
1200 npurged == 1 ? "" : "s");
1202 printf("; purged %d object%s", npurged,
1203 npurged == 1 ? "" : "s");
1206 if (print_redundant) {
1208 printf(", %d pack file%s", nredundant,
1209 nredundant == 1 ? "" : "s");
1211 printf(", %d pack file%s", nredundant,
1212 nredundant == 1 ? "" : "s");
1215 if (print_loose || print_commits || print_purged || print_redundant) {
1216 a->printed_something = 1;
1222 struct got_lonely_packidx_progress_arg {
1224 int printed_something;
1228 static const struct got_error *
1229 lonely_packidx_progress(void *arg, const char *path)
1231 struct got_lonely_packidx_progress_arg *a = arg;
1233 if (a->verbosity < 0)
1237 printf("%s could be removed\n", path);
1239 printf("%s removed\n", path);
1241 a->printed_something = 1;
1245 static const struct got_error *
1246 cmd_cleanup(int argc, char *argv[])
1248 const struct got_error *error = NULL;
1249 char *repo_path = NULL;
1250 struct got_repository *repo = NULL;
1251 int ch, dry_run = 0, verbosity = 0;
1252 int ncommits = 0, nloose = 0, npacked = 0;
1253 int remove_lonely_packidx = 0, ignore_mtime = 0;
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 if (remove_lonely_packidx) {
1326 memset(&lpa, 0, sizeof(lpa));
1327 lpa.dry_run = dry_run;
1328 lpa.verbosity = verbosity;
1329 error = got_repo_remove_lonely_packidx(repo, dry_run,
1330 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1334 memset(&cpa, 0, sizeof(cpa));
1335 cpa.last_nloose = -1;
1336 cpa.last_npurged = -1;
1337 cpa.last_nredundant = -1;
1338 cpa.dry_run = dry_run;
1339 cpa.verbosity = verbosity;
1341 error = got_repo_cleanup(repo, &loose_before, &loose_after,
1342 &pack_before, &pack_after, &ncommits, &nloose, &npacked,
1343 dry_run, ignore_mtime, cleanup_progress, &cpa,
1344 check_cancelled, NULL);
1345 if (cpa.printed_something)
1350 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1352 if (cpa.printed_something) {
1353 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1354 error = got_error_from_errno("fmt_scaled");
1357 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1358 error = got_error_from_errno("fmt_scaled");
1361 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1362 error = got_error_from_errno("fmt_scaled");
1365 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1366 error = got_error_from_errno("fmt_scaled");
1369 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1370 error = got_error_from_errno("fmt_scaled");
1373 printf("loose total size before: %s\n", loose_before_scaled);
1374 printf("loose total size after: %s\n", loose_after_scaled);
1375 printf("pack files total size before: %s\n",
1376 pack_before_scaled);
1377 printf("pack files total size after: %s\n", pack_after_scaled);
1379 printf("disk space which would be freed: %s\n",
1382 printf("disk space freed: %s\n", total_size_scaled);
1383 printf("loose objects also found in pack files: %d\n", npacked);
1388 got_repo_close(repo);
1390 const struct got_error *pack_err =
1391 got_repo_pack_fds_close(pack_fds);
1402 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1403 "[-x reference] [reference]...\n", getprogname());
1407 static const struct got_error *
1408 cmd_dump(int argc, char *argv[])
1410 const struct got_error *error = NULL;
1411 struct got_pack_progress_arg ppa;
1412 struct got_repository *repo = NULL;
1413 struct got_pathlist_head exclude_args;
1414 struct got_pathlist_entry *pe;
1415 struct got_reflist_head exclude_refs;
1416 struct got_reflist_head include_refs;
1417 struct got_reflist_entry *re, *new;
1418 const char *refname;
1419 char *repo_path = NULL;
1420 int *pack_fds = NULL;
1424 TAILQ_INIT(&exclude_args);
1425 TAILQ_INIT(&exclude_refs);
1426 TAILQ_INIT(&include_refs);
1429 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1434 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1440 repo_path = realpath(optarg, NULL);
1441 if (repo_path == NULL)
1442 return got_error_from_errno2("realpath",
1444 got_path_strip_trailing_slashes(repo_path);
1447 error = got_pathlist_append(&exclude_args,
1460 if (repo_path == NULL) {
1461 error = get_repo_path(&repo_path);
1465 error = got_repo_pack_fds_open(&pack_fds);
1468 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1472 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1476 TAILQ_FOREACH(pe, &exclude_args, entry) {
1478 error = add_ref(&new, &exclude_refs, refname, repo);
1484 error = got_ref_list(&include_refs, repo, "",
1485 got_ref_cmp_by_name, NULL);
1489 for (i = 0; i < argc; i++) {
1490 got_path_strip_trailing_slashes(argv[i]);
1492 error = add_ref(&new, &include_refs, refname, repo);
1498 /* Ignore references in the refs/got/ namespace. */
1499 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1500 refname = got_ref_get_name(re->ref);
1501 if (strncmp("refs/got/", refname, 9) != 0)
1503 TAILQ_REMOVE(&include_refs, re, entry);
1504 got_ref_close(re->ref);
1508 memset(&ppa, 0, sizeof(ppa));
1510 ppa.verbosity = verbosity;
1512 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1513 repo, pack_progress, &ppa, check_cancelled, NULL);
1514 if (ppa.printed_something)
1515 fprintf(stderr, "\n");
1518 got_repo_close(repo);
1521 const struct got_error *pack_err;
1523 pack_err = got_repo_pack_fds_close(pack_fds);
1528 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1529 got_ref_list_free(&exclude_refs);
1530 got_ref_list_free(&include_refs);
1539 fprintf(stderr, "usage: %s load [-nq] [-l bundle-file] "
1540 "[-r repository-path] [reference ...]\n",
1545 static const struct got_error *
1546 load_progress(void *arg, off_t packfile_size, int nobj_total,
1547 int nobj_indexed, int nobj_loose, int nobj_resolved)
1549 return pack_index_progress(arg, packfile_size, nobj_total,
1550 nobj_indexed, nobj_loose, nobj_resolved);
1554 is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1556 struct got_pathlist_entry *pe;
1558 if (TAILQ_EMPTY(wanted))
1561 TAILQ_FOREACH(pe, wanted, entry) {
1562 if (strcmp(pe->path, ref) == 0)
1569 static const struct got_error *
1570 create_ref(const char *refname, struct got_object_id *id,
1571 int verbosity, struct got_repository *repo)
1573 const struct got_error *err = NULL;
1574 struct got_reference *ref;
1577 err = got_object_id_str(&id_str, id);
1581 err = got_ref_alloc(&ref, refname, id);
1585 err = got_ref_write(ref, repo);
1588 if (err == NULL && verbosity >= 0)
1589 printf("Created reference %s: %s\n", refname, id_str);
1595 static const struct got_error *
1596 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1597 int replace_tags, int verbosity, struct got_repository *repo)
1599 const struct got_error *err = NULL;
1600 char *new_id_str = NULL;
1601 struct got_object_id *old_id = NULL;
1603 err = got_object_id_str(&new_id_str, new_id);
1607 if (!replace_tags &&
1608 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1609 err = got_ref_resolve(&old_id, repo, ref);
1612 if (got_object_id_cmp(old_id, new_id) == 0)
1614 if (verbosity >= 0) {
1615 printf("Rejecting update of existing tag %s: %s\n",
1616 got_ref_get_name(ref), new_id_str);
1621 if (got_ref_is_symbolic(ref)) {
1622 if (verbosity >= 0) {
1623 printf("Replacing reference %s: %s\n",
1624 got_ref_get_name(ref),
1625 got_ref_get_symref_target(ref));
1627 err = got_ref_change_symref_to_ref(ref, new_id);
1630 err = got_ref_write(ref, repo);
1634 err = got_ref_resolve(&old_id, repo, ref);
1637 if (got_object_id_cmp(old_id, new_id) == 0)
1640 err = got_ref_change_ref(ref, new_id);
1643 err = got_ref_write(ref, repo);
1649 printf("Updated %s: %s\n", got_ref_get_name(ref),
1657 static const struct got_error *
1658 cmd_load(int argc, char *argv[])
1660 const struct got_error *error = NULL;
1661 struct got_repository *repo = NULL;
1662 struct got_pathlist_head include_args;
1663 struct got_pathlist_head available_refs;
1664 struct got_pathlist_entry *pe;
1665 struct got_pack_progress_arg ppa;
1667 int *pack_fds = NULL;
1668 char *repo_path = NULL;
1669 int list_refs_only = 0;
1674 TAILQ_INIT(&include_args);
1675 TAILQ_INIT(&available_refs);
1678 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1679 "sendfd unveil", NULL) == -1)
1683 while ((ch = getopt(argc, argv, "l:nqr:")) != -1) {
1687 in = fopen(optarg, "re");
1689 return got_error_from_errno2("open", optarg);
1698 repo_path = realpath(optarg, NULL);
1699 if (repo_path == NULL)
1700 return got_error_from_errno2("realpath",
1702 got_path_strip_trailing_slashes(repo_path);
1712 if (list_refs_only && argc > 1)
1713 errx(1, "-l and references on the command line are exclusive");
1714 if (list_refs_only && noop)
1715 errx(1, "-n and -l are mutually exclusive");
1717 for (i = 0; i < argc; i++) {
1718 char *refname = argv[i];
1719 got_path_strip_trailing_slashes(refname);
1720 if (!got_ref_name_is_valid(refname))
1721 errx(1, "invalid reference name %s", refname);
1722 error = got_pathlist_append(&include_args, refname, NULL);
1727 if (repo_path == NULL) {
1728 error = get_repo_path(&repo_path);
1732 error = got_repo_pack_fds_open(&pack_fds);
1735 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1739 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1743 memset(&ppa, 0, sizeof(ppa));
1745 ppa.verbosity = verbosity;
1747 error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1748 load_progress, &ppa, check_cancelled, NULL);
1749 if (verbosity >= 0 && !list_refs_only)
1754 if (list_refs_only) {
1755 TAILQ_FOREACH(pe, &available_refs, entry) {
1756 const char *refname = pe->path;
1757 struct got_object_id *id = pe->data;
1760 error = got_object_id_str(&idstr, id);
1764 printf("%s: %s\n", refname, idstr);
1773 /* Update references */
1774 TAILQ_FOREACH(pe, &available_refs, entry) {
1775 const struct got_error *unlock_err;
1776 struct got_reference *ref;
1777 const char *refname = pe->path;
1778 struct got_object_id *id = pe->data;
1780 if (!is_wanted_ref(&include_args, pe->path))
1783 error = got_ref_open(&ref, repo, refname, 1);
1785 if (error->code != GOT_ERR_NOT_REF)
1787 error = create_ref(refname, id, verbosity, repo);
1791 /* XXX: check advances only and add -f to force? */
1792 error = update_ref(ref, id, 1, verbosity, repo);
1793 unlock_err = got_ref_unlock(ref);
1794 if (unlock_err && error == NULL)
1803 if (in != stdin && fclose(in) == EOF && error == NULL)
1804 error = got_error_from_errno("fclose");
1807 got_repo_close(repo);
1810 const struct got_error *pack_err;
1812 pack_err = got_repo_pack_fds_close(pack_fds);
1817 got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1818 got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);