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>
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"
43 #include "got_privsep.h"
44 #include "got_opentemp.h"
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
54 catch_sigint(int signo)
60 catch_sigpipe(int signo)
65 static const struct got_error *
66 check_cancelled(void *arg)
68 if (sigint_received || sigpipe_received)
69 return got_error(GOT_ERR_CANCELLED);
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
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);
86 static const struct got_error* cmd_info(int, char *[]);
87 static const struct got_error* cmd_pack(int, char *[]);
88 static const struct got_error* cmd_indexpack(int, char *[]);
89 static const struct got_error* cmd_listpack(int, char *[]);
91 static struct gotadmin_cmd gotadmin_commands[] = {
92 { "info", cmd_info, usage_info, "" },
93 { "pack", cmd_pack, usage_pack, "" },
94 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
95 { "listpack", cmd_listpack, usage_listpack, "ls" },
99 list_commands(FILE *fp)
103 fprintf(fp, "commands:");
104 for (i = 0; i < nitems(gotadmin_commands); i++) {
105 struct gotadmin_cmd *cmd = &gotadmin_commands[i];
106 fprintf(fp, " %s", cmd->cmd_name);
112 main(int argc, char *argv[])
114 struct gotadmin_cmd *cmd;
117 int hflag = 0, Vflag = 0;
118 static struct option longopts[] = {
119 { "version", no_argument, NULL, 'V' },
123 setlocale(LC_CTYPE, "");
125 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
145 got_version_print_str();
150 usage(hflag, hflag ? 0 : 1);
152 signal(SIGINT, catch_sigint);
153 signal(SIGPIPE, catch_sigpipe);
155 for (i = 0; i < nitems(gotadmin_commands); i++) {
156 const struct got_error *error;
158 cmd = &gotadmin_commands[i];
160 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
161 strcmp(cmd->cmd_alias, argv[0]) != 0)
165 gotadmin_commands[i].cmd_usage();
167 error = gotadmin_commands[i].cmd_main(argc, argv);
168 if (error && error->code != GOT_ERR_CANCELLED &&
169 error->code != GOT_ERR_PRIVSEP_EXIT &&
170 !(sigpipe_received &&
171 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
173 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 list_commands(stderr);
187 usage(int hflag, int status)
189 FILE *fp = (status == 0) ? stdout : stderr;
191 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
198 static const struct got_error *
199 apply_unveil(const char *repo_path, int repo_read_only)
201 const struct got_error *err;
204 if (unveil("gmon.out", "rwc") != 0)
205 return got_error_from_errno2("unveil", "gmon.out");
207 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
208 return got_error_from_errno2("unveil", repo_path);
210 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
211 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
213 err = got_privsep_unveil_exec_helpers();
217 if (unveil(NULL, NULL) != 0)
218 return got_error_from_errno("unveil");
226 fprintf(stderr, "usage: %s info [-r repository-path]\n",
231 static const struct got_error *
232 cmd_info(int argc, char *argv[])
234 const struct got_error *error = NULL;
235 char *cwd = NULL, *repo_path = NULL;
236 struct got_repository *repo = NULL;
237 const struct got_gotconfig *gotconfig = NULL;
238 int ch, npackfiles, npackedobj, nobj;
239 off_t packsize, loose_size;
240 char scaled[FMT_SCALED_STRSIZE];
242 while ((ch = getopt(argc, argv, "r:")) != -1) {
245 repo_path = realpath(optarg, NULL);
246 if (repo_path == NULL)
247 return got_error_from_errno2("realpath",
249 got_path_strip_trailing_slashes(repo_path);
261 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
265 cwd = getcwd(NULL, 0);
267 error = got_error_from_errno("getcwd");
271 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
275 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
279 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
281 gotconfig = got_repo_get_gotconfig(repo);
283 const struct got_remote_repo *remotes;
285 if (got_gotconfig_get_author(gotconfig)) {
286 printf("default author: %s\n",
287 got_gotconfig_get_author(gotconfig));
289 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
290 for (i = 0; i < nremotes; i++) {
291 printf("remote \"%s\": %s\n", remotes[i].name,
296 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
300 printf("pack files: %d\n", npackfiles);
301 if (npackfiles > 0) {
302 if (fmt_scaled(packsize, scaled) == -1) {
303 error = got_error_from_errno("fmt_scaled");
306 printf("packed objects: %d\n", npackedobj);
307 printf("packed total size: %s\n", scaled);
310 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
313 printf("loose objects: %d\n", nobj);
315 if (fmt_scaled(loose_size, scaled) == -1) {
316 error = got_error_from_errno("fmt_scaled");
319 printf("loose total size: %s\n", scaled);
323 got_repo_close(repo);
331 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
332 "[-x reference] [reference ...]\n",
337 struct got_pack_progress_arg {
338 char last_scaled_size[FMT_SCALED_STRSIZE];
346 int printed_something;
349 static const struct got_error *
350 pack_progress(void *arg, off_t packfile_size, int ncommits,
351 int nobj_total, int nobj_deltify, int nobj_written)
353 struct got_pack_progress_arg *a = arg;
354 char scaled_size[FMT_SCALED_STRSIZE];
355 int p_deltify, p_written;
356 int print_searching = 0, print_total = 0;
357 int print_deltify = 0, print_written = 0;
359 if (a->verbosity < 0)
362 if (fmt_scaled(packfile_size, scaled_size) == -1)
363 return got_error_from_errno("fmt_scaled");
365 if (a->last_ncommits != ncommits) {
367 a->last_ncommits = ncommits;
370 if (a->last_nobj_total != nobj_total) {
373 a->last_nobj_total = nobj_total;
376 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
377 strcmp(scaled_size, a->last_scaled_size)) != 0) {
378 if (strlcpy(a->last_scaled_size, scaled_size,
379 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
380 return got_error(GOT_ERR_NO_SPACE);
383 if (nobj_deltify > 0 || nobj_written > 0) {
384 if (nobj_deltify > 0) {
385 p_deltify = (nobj_deltify * 100) / nobj_total;
386 if (p_deltify != a->last_p_deltify) {
387 a->last_p_deltify = p_deltify;
393 if (nobj_written > 0) {
394 p_written = (nobj_written * 100) / nobj_total;
395 if (p_written != a->last_p_written) {
396 a->last_p_written = p_written;
405 if (print_searching || print_total || print_deltify || print_written)
408 printf("packing %d reference%s", ncommits,
409 ncommits == 1 ? "" : "s");
411 printf("; %d object%s", nobj_total,
412 nobj_total == 1 ? "" : "s");
414 printf("; deltify: %d%%", p_deltify);
416 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
417 scaled_size, p_written);
418 if (print_searching || print_total || print_deltify ||
420 a->printed_something = 1;
426 static const struct got_error *
427 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
428 int nobj_indexed, int nobj_loose, int nobj_resolved)
430 struct got_pack_progress_arg *a = arg;
431 char scaled_size[FMT_SCALED_STRSIZE];
432 int p_indexed, p_resolved;
433 int print_size = 0, print_indexed = 0, print_resolved = 0;
435 if (a->verbosity < 0)
438 if (packfile_size > 0 || nobj_indexed > 0) {
439 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
440 (a->last_scaled_size[0] == '\0' ||
441 strcmp(scaled_size, a->last_scaled_size)) != 0) {
443 if (strlcpy(a->last_scaled_size, scaled_size,
444 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
445 return got_error(GOT_ERR_NO_SPACE);
447 if (nobj_indexed > 0) {
448 p_indexed = (nobj_indexed * 100) / nobj_total;
449 if (p_indexed != a->last_p_indexed) {
450 a->last_p_indexed = p_indexed;
455 if (nobj_resolved > 0) {
456 p_resolved = (nobj_resolved * 100) /
457 (nobj_total - nobj_loose);
458 if (p_resolved != a->last_p_resolved) {
459 a->last_p_resolved = p_resolved;
467 if (print_size || print_indexed || print_resolved)
470 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
472 printf("; indexing %d%%", p_indexed);
474 printf("; resolving deltas %d%%", p_resolved);
475 if (print_size || print_indexed || print_resolved)
481 static const struct got_error *
482 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
483 const char *refname, struct got_repository *repo)
485 const struct got_error *err;
486 struct got_reference *ref;
490 err = got_ref_open(&ref, repo, refname, 0);
492 if (err->code != GOT_ERR_NOT_REF)
495 /* Treat argument as a reference prefix. */
496 err = got_ref_list(refs, repo, refname,
497 got_ref_cmp_by_name, NULL);
499 err = got_reflist_insert(new, refs, ref, repo,
500 got_ref_cmp_by_name, NULL);
501 if (err || *new == NULL /* duplicate */)
508 static const struct got_error *
509 cmd_pack(int argc, char *argv[])
511 const struct got_error *error = NULL;
512 char *cwd = NULL, *repo_path = NULL;
513 struct got_repository *repo = NULL;
514 int ch, i, loose_obj_only = 1;
515 struct got_object_id *pack_hash = NULL;
517 struct got_pack_progress_arg ppa;
518 FILE *packfile = NULL;
519 struct got_pathlist_head exclude_args;
520 struct got_pathlist_entry *pe;
521 struct got_reflist_head exclude_refs;
522 struct got_reflist_head include_refs;
523 struct got_reflist_entry *re, *new;
525 TAILQ_INIT(&exclude_args);
526 TAILQ_INIT(&exclude_refs);
527 TAILQ_INIT(&include_refs);
529 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
535 repo_path = realpath(optarg, NULL);
536 if (repo_path == NULL)
537 return got_error_from_errno2("realpath",
539 got_path_strip_trailing_slashes(repo_path);
542 got_path_strip_trailing_slashes(optarg);
543 error = got_pathlist_append(&exclude_args,
558 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
562 cwd = getcwd(NULL, 0);
564 error = got_error_from_errno("getcwd");
568 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
572 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
576 TAILQ_FOREACH(pe, &exclude_args, entry) {
577 const char *refname = pe->path;
578 error = add_ref(&new, &exclude_refs, refname, repo);
585 error = got_ref_list(&include_refs, repo, "",
586 got_ref_cmp_by_name, NULL);
590 for (i = 0; i < argc; i++) {
592 got_path_strip_trailing_slashes(argv[i]);
594 error = add_ref(&new, &include_refs, refname, repo);
600 /* Ignore references in the refs/got/ namespace. */
601 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
602 const char *refname = got_ref_get_name(re->ref);
603 if (strncmp("refs/got/", refname, 9) != 0)
605 TAILQ_REMOVE(&include_refs, re, entry);
606 got_ref_close(re->ref);
610 memset(&ppa, 0, sizeof(ppa));
611 ppa.last_scaled_size[0] = '\0';
612 ppa.last_p_indexed = -1;
613 ppa.last_p_resolved = -1;
615 error = got_repo_pack_objects(&packfile, &pack_hash,
616 &include_refs, &exclude_refs, repo, loose_obj_only,
617 pack_progress, &ppa, check_cancelled, NULL);
619 if (ppa.printed_something)
624 error = got_object_id_str(&id_str, pack_hash);
627 printf("\nWrote %s.pack\n", id_str);
629 error = got_repo_index_pack(packfile, pack_hash, repo,
630 pack_index_progress, &ppa, check_cancelled, NULL);
633 printf("\nIndexed %s.pack\n", id_str);
635 got_pathlist_free(&exclude_args);
636 got_ref_list_free(&exclude_refs);
637 got_ref_list_free(&include_refs);
645 usage_indexpack(void)
647 fprintf(stderr, "usage: %s indexpack packfile-path\n",
652 static const struct got_error *
653 cmd_indexpack(int argc, char *argv[])
655 const struct got_error *error = NULL;
656 struct got_repository *repo = NULL;
658 struct got_object_id *pack_hash = NULL;
659 char *packfile_path = NULL;
661 struct got_pack_progress_arg ppa;
662 FILE *packfile = NULL;
664 while ((ch = getopt(argc, argv, "")) != -1) {
678 packfile_path = realpath(argv[0], NULL);
679 if (packfile_path == NULL)
680 return got_error_from_errno2("realpath", argv[0]);
683 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
688 error = got_repo_open(&repo, packfile_path, NULL);
692 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
696 memset(&ppa, 0, sizeof(ppa));
697 ppa.last_scaled_size[0] = '\0';
698 ppa.last_p_indexed = -1;
699 ppa.last_p_resolved = -1;
701 error = got_repo_find_pack(&packfile, &pack_hash, repo,
706 error = got_object_id_str(&id_str, pack_hash);
710 error = got_repo_index_pack(packfile, pack_hash, repo,
711 pack_index_progress, &ppa, check_cancelled, NULL);
714 printf("\nIndexed %s.pack\n", id_str);
724 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
729 struct gotadmin_list_pack_cb_args {
739 static const struct got_error *
740 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
741 off_t size, off_t base_offset, struct got_object_id *base_id)
743 const struct got_error *err;
744 struct gotadmin_list_pack_cb_args *a = arg;
745 char *id_str, *delta_str = NULL, *base_id_str = NULL;
746 const char *type_str;
748 err = got_object_id_str(&id_str, id);
753 case GOT_OBJ_TYPE_BLOB:
754 type_str = GOT_OBJ_LABEL_BLOB;
757 case GOT_OBJ_TYPE_TREE:
758 type_str = GOT_OBJ_LABEL_TREE;
761 case GOT_OBJ_TYPE_COMMIT:
762 type_str = GOT_OBJ_LABEL_COMMIT;
765 case GOT_OBJ_TYPE_TAG:
766 type_str = GOT_OBJ_LABEL_TAG;
769 case GOT_OBJ_TYPE_OFFSET_DELTA:
770 type_str = "offset-delta";
771 if (asprintf(&delta_str, " base-offset %llu",
772 base_offset) == -1) {
773 err = got_error_from_errno("asprintf");
778 case GOT_OBJ_TYPE_REF_DELTA:
779 type_str = "ref-delta";
780 err = got_object_id_str(&base_id_str, base_id);
783 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
784 err = got_error_from_errno("asprintf");
790 err = got_error(GOT_ERR_OBJ_TYPE);
793 if (a->human_readable) {
794 char scaled[FMT_SCALED_STRSIZE];
796 if (fmt_scaled(size, scaled) == -1) {
797 err = got_error_from_errno("fmt_scaled");
801 while (isspace((unsigned char)*s))
803 printf("%s %s at %llu size %s%s\n", id_str, type_str, offset,
804 s, delta_str ? delta_str : "");
806 printf("%s %s at %llu size %llu%s\n", id_str, type_str, offset,
807 size, delta_str ? delta_str : "");
816 static const struct got_error *
817 cmd_listpack(int argc, char *argv[])
819 const struct got_error *error = NULL;
820 struct got_repository *repo = NULL;
822 struct got_object_id *pack_hash = NULL;
823 char *packfile_path = NULL;
825 struct gotadmin_list_pack_cb_args lpa;
826 FILE *packfile = NULL;
827 int show_stats = 0, human_readable = 0;
829 while ((ch = getopt(argc, argv, "hs")) != -1) {
848 packfile_path = realpath(argv[0], NULL);
849 if (packfile_path == NULL)
850 return got_error_from_errno2("realpath", argv[0]);
853 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
857 error = got_repo_open(&repo, packfile_path, NULL);
861 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
865 error = got_repo_find_pack(&packfile, &pack_hash, repo,
869 error = got_object_id_str(&id_str, pack_hash);
873 memset(&lpa, 0, sizeof(lpa));
874 lpa.human_readable = human_readable;
875 error = got_repo_list_pack(packfile, pack_hash, repo,
876 list_pack_cb, &lpa, check_cancelled, NULL);
880 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
881 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
882 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
883 lpa.noffdeltas + lpa.nrefdeltas,
884 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
885 lpa.noffdeltas, lpa.nrefdeltas);