bump version number
[got-portable.git] / gotadmin / gotadmin.c
blob7721c6c239557c9accd80b3bab5d98a3f57b1ddb
1 /*
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>
22 #include <ctype.h>
23 #include <getopt.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.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"
44 #include "got_path.h"
45 #include "got_privsep.h"
46 #include "got_opentemp.h"
47 #include "got_worktree.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
56 static void
57 catch_sigint(int signo)
59 sigint_received = 1;
62 static void
63 catch_sigpipe(int signo)
65 sigpipe_received = 1;
68 static const struct got_error *
69 check_cancelled(void *arg)
71 if (sigint_received || sigpipe_received)
72 return got_error(GOT_ERR_CANCELLED);
73 return NULL;
76 struct gotadmin_cmd {
77 const char *cmd_name;
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, "" },
113 static void
114 list_commands(FILE *fp)
116 size_t i;
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);
123 fputc('\n', fp);
127 main(int argc, char *argv[])
129 const struct gotadmin_cmd *cmd;
130 size_t i;
131 int ch;
132 int hflag = 0, Vflag = 0;
133 static const struct option longopts[] = {
134 { "version", no_argument, NULL, 'V' },
135 { NULL, 0, NULL, 0 }
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
141 switch (ch) {
142 case 'h':
143 hflag = 1;
144 break;
145 case 'V':
146 Vflag = 1;
147 break;
148 default:
149 usage(hflag, 1);
150 /* NOTREACHED */
154 argc -= optind;
155 argv += optind;
156 optind = 1;
157 optreset = 1;
159 if (Vflag) {
160 got_version_print_str();
161 return 0;
164 if (argc <= 0)
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)
177 continue;
179 if (hflag)
180 cmd->cmd_usage();
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) &&
187 !(sigint_received &&
188 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
189 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
190 return 1;
193 return 0;
196 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
197 list_commands(stderr);
198 return 1;
201 __dead static void
202 usage(int hflag, int status)
204 FILE *fp = (status == 0) ? stdout : stderr;
206 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
207 getprogname());
208 if (hflag)
209 list_commands(fp);
210 exit(status);
213 static const struct got_error *
214 apply_unveil(const char *repo_path, int repo_read_only)
216 const struct got_error *err;
218 #ifdef PROFILE
219 if (unveil("gmon.out", "rwc") != 0)
220 return got_error_from_errno2("unveil", "gmon.out");
221 #endif
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();
229 if (err != NULL)
230 return err;
232 if (unveil(NULL, NULL) != 0)
233 return got_error_from_errno("unveil");
235 return NULL;
238 __dead static void
239 usage_info(void)
241 fprintf(stderr, "usage: %s info [-r repository-path]\n",
242 getprogname());
243 exit(1);
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;
251 char *cwd;
253 *repo_path = NULL;
255 cwd = getcwd(NULL, 0);
256 if (cwd == NULL)
257 return got_error_from_errno("getcwd");
259 err = got_worktree_open(&worktree, cwd, NULL);
260 if (err) {
261 if (err->code != GOT_ERR_NOT_WORKTREE)
262 goto done;
263 err = NULL;
266 if (worktree)
267 *repo_path = strdup(got_worktree_get_repo_path(worktree));
268 else
269 *repo_path = strdup(cwd);
270 if (*repo_path == NULL)
271 err = got_error_from_errno("strdup");
272 done:
273 if (worktree)
274 got_worktree_close(worktree);
275 free(cwd);
276 return err;
279 __dead static void
280 usage_init(void)
282 fprintf(stderr, "usage: %s init [-A hashing-algorithm] [-b branch]"
283 " repository-path\n",
284 getprogname());
285 exit(1);
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;
294 enum got_hash_algorithm algo = GOT_HASH_SHA1;
295 int ch;
297 #ifndef PROFILE
298 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
299 err(1, "pledge");
300 #endif
302 while ((ch = getopt(argc, argv, "A:b:")) != -1) {
303 switch (ch) {
304 case 'A':
305 if (!strcmp(optarg, "sha1"))
306 algo = GOT_HASH_SHA1;
307 else if (!strcmp(optarg, "sha256"))
308 algo = GOT_HASH_SHA256;
309 else
310 return got_error_path(optarg,
311 GOT_ERR_OBJECT_FORMAT);
312 break;
313 case 'b':
314 head_name = optarg;
315 break;
316 default:
317 usage_init();
318 /* NOTREACHED */
322 argc -= optind;
323 argv += optind;
325 if (argc != 1)
326 usage_init();
328 repo_path = strdup(argv[0]);
329 if (repo_path == NULL)
330 return got_error_from_errno("strdup");
332 got_path_strip_trailing_slashes(repo_path);
334 error = got_path_mkdir(repo_path);
335 if (error &&
336 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
337 goto done;
339 error = apply_unveil(repo_path, 0);
340 if (error)
341 goto done;
343 error = got_repo_init(repo_path, head_name, algo);
344 done:
345 free(repo_path);
346 return error;
349 static const struct got_error *
350 cmd_info(int argc, char *argv[])
352 const struct got_error *error = NULL;
353 char *repo_path = NULL;
354 struct got_repository *repo = NULL;
355 const struct got_gotconfig *gotconfig = NULL;
356 int ch, npackfiles, npackedobj, nobj;
357 off_t packsize, loose_size;
358 char scaled[FMT_SCALED_STRSIZE];
359 int *pack_fds = NULL;
361 #ifndef PROFILE
362 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
363 NULL) == -1)
364 err(1, "pledge");
365 #endif
367 while ((ch = getopt(argc, argv, "r:")) != -1) {
368 switch (ch) {
369 case 'r':
370 repo_path = realpath(optarg, NULL);
371 if (repo_path == NULL)
372 return got_error_from_errno2("realpath",
373 optarg);
374 got_path_strip_trailing_slashes(repo_path);
375 break;
376 default:
377 usage_info();
378 /* NOTREACHED */
382 argc -= optind;
383 argv += optind;
385 if (repo_path == NULL) {
386 error = get_repo_path(&repo_path);
387 if (error)
388 goto done;
390 error = got_repo_pack_fds_open(&pack_fds);
391 if (error != NULL)
392 goto done;
393 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
394 if (error)
395 goto done;
396 #ifndef PROFILE
397 /* Remove "cpath" promise. */
398 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
399 NULL) == -1)
400 err(1, "pledge");
401 #endif
402 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
403 if (error)
404 goto done;
406 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
407 printf("hashing algorithm: %s\n",
408 got_repo_get_object_format(repo) == GOT_HASH_SHA1 ? "sha1"
409 : "sha256");
411 gotconfig = got_repo_get_gotconfig(repo);
412 if (gotconfig) {
413 const struct got_remote_repo *remotes;
414 int i, nremotes;
415 if (got_gotconfig_get_author(gotconfig)) {
416 printf("default author: %s\n",
417 got_gotconfig_get_author(gotconfig));
419 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
420 for (i = 0; i < nremotes; i++) {
421 const char *fetch_url = remotes[i].fetch_url;
422 const char *send_url = remotes[i].send_url;
423 if (strcmp(fetch_url, send_url) == 0) {
424 printf("remote \"%s\": %s\n", remotes[i].name,
425 remotes[i].fetch_url);
426 } else {
427 printf("remote \"%s\" (fetch): %s\n",
428 remotes[i].name, remotes[i].fetch_url);
429 printf("remote \"%s\" (send): %s\n",
430 remotes[i].name, remotes[i].send_url);
435 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
436 &packsize, repo);
437 if (error)
438 goto done;
439 printf("pack files: %d\n", npackfiles);
440 if (npackfiles > 0) {
441 if (fmt_scaled(packsize, scaled) == -1) {
442 error = got_error_from_errno("fmt_scaled");
443 goto done;
445 printf("packed objects: %d\n", npackedobj);
446 printf("packed total size: %s\n", scaled);
449 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
450 if (error)
451 goto done;
452 printf("loose objects: %d\n", nobj);
453 if (nobj > 0) {
454 if (fmt_scaled(loose_size, scaled) == -1) {
455 error = got_error_from_errno("fmt_scaled");
456 goto done;
458 printf("loose total size: %s\n", scaled);
460 done:
461 if (repo)
462 got_repo_close(repo);
463 if (pack_fds) {
464 const struct got_error *pack_err =
465 got_repo_pack_fds_close(pack_fds);
466 if (error == NULL)
467 error = pack_err;
470 free(repo_path);
471 return error;
474 __dead static void
475 usage_pack(void)
477 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
478 "[-x reference] [reference ...]\n", getprogname());
479 exit(1);
482 struct got_pack_progress_arg {
483 FILE *out;
484 char last_scaled_size[FMT_SCALED_STRSIZE];
485 int last_ncolored;
486 int last_nfound;
487 int last_ntrees;
488 int loading_done;
489 int last_ncommits;
490 int last_nobj_total;
491 int last_p_deltify;
492 int last_p_written;
493 int last_p_indexed;
494 int last_p_resolved;
495 int verbosity;
496 int printed_something;
499 static void
500 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
501 int ncolored, int nfound, int ntrees)
503 if (print_colored) {
504 fprintf(out, "%d commit%s colored", ncolored,
505 ncolored == 1 ? "" : "s");
507 if (print_found) {
508 fprintf(out, "%s%d object%s found",
509 ncolored > 0 ? "; " : "",
510 nfound, nfound == 1 ? "" : "s");
512 if (print_trees) {
513 fprintf(out, "; %d tree%s scanned", ntrees,
514 ntrees == 1 ? "" : "s");
518 static const struct got_error *
519 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
520 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
521 int nobj_written)
523 struct got_pack_progress_arg *a = arg;
524 char scaled_size[FMT_SCALED_STRSIZE];
525 int p_deltify, p_written;
526 int print_colored = 0, print_found = 0, print_trees = 0;
527 int print_searching = 0, print_total = 0;
528 int print_deltify = 0, print_written = 0;
530 if (a->verbosity < 0)
531 return NULL;
533 if (a->last_ncolored != ncolored) {
534 print_colored = 1;
535 a->last_ncolored = ncolored;
538 if (a->last_nfound != nfound) {
539 print_colored = 1;
540 print_found = 1;
541 a->last_nfound = nfound;
544 if (a->last_ntrees != ntrees) {
545 print_colored = 1;
546 print_found = 1;
547 print_trees = 1;
548 a->last_ntrees = ntrees;
551 if ((print_colored || print_found || print_trees) &&
552 !a->loading_done) {
553 fprintf(a->out, "\r");
554 print_load_info(a->out, print_colored, print_found,
555 print_trees, ncolored, nfound, ntrees);
556 a->printed_something = 1;
557 fflush(a->out);
558 return NULL;
559 } else if (!a->loading_done) {
560 fprintf(a->out, "\r");
561 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
562 fprintf(a->out, "\n");
563 a->loading_done = 1;
566 if (fmt_scaled(packfile_size, scaled_size) == -1)
567 return got_error_from_errno("fmt_scaled");
569 if (a->last_ncommits != ncommits) {
570 print_searching = 1;
571 a->last_ncommits = ncommits;
574 if (a->last_nobj_total != nobj_total) {
575 print_searching = 1;
576 print_total = 1;
577 a->last_nobj_total = nobj_total;
580 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
581 strcmp(scaled_size, a->last_scaled_size)) != 0) {
582 if (strlcpy(a->last_scaled_size, scaled_size,
583 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
584 return got_error(GOT_ERR_NO_SPACE);
587 if (nobj_deltify > 0 || nobj_written > 0) {
588 if (nobj_deltify > 0) {
589 p_deltify = (nobj_deltify * 100) / nobj_total;
590 if (p_deltify != a->last_p_deltify) {
591 a->last_p_deltify = p_deltify;
592 print_searching = 1;
593 print_total = 1;
594 print_deltify = 1;
597 if (nobj_written > 0) {
598 p_written = (nobj_written * 100) / nobj_total;
599 if (p_written != a->last_p_written) {
600 a->last_p_written = p_written;
601 print_searching = 1;
602 print_total = 1;
603 print_deltify = 1;
604 print_written = 1;
609 if (print_searching || print_total || print_deltify || print_written)
610 fprintf(a->out, "\r");
611 if (print_searching)
612 fprintf(a->out, "packing %d reference%s", ncommits,
613 ncommits == 1 ? "" : "s");
614 if (print_total)
615 fprintf(a->out, "; %d object%s", nobj_total,
616 nobj_total == 1 ? "" : "s");
617 if (print_deltify)
618 fprintf(a->out, "; deltify: %d%%", p_deltify);
619 if (print_written)
620 fprintf(a->out, "; writing pack: %*s %d%%",
621 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
622 if (print_searching || print_total || print_deltify ||
623 print_written) {
624 a->printed_something = 1;
625 fflush(a->out);
627 return NULL;
630 static const struct got_error *
631 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
632 int nobj_indexed, int nobj_loose, int nobj_resolved)
634 struct got_pack_progress_arg *a = arg;
635 char scaled_size[FMT_SCALED_STRSIZE];
636 int p_indexed, p_resolved;
637 int print_size = 0, print_indexed = 0, print_resolved = 0;
639 if (a->verbosity < 0)
640 return NULL;
642 if (packfile_size > 0 || nobj_indexed > 0) {
643 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
644 (a->last_scaled_size[0] == '\0' ||
645 strcmp(scaled_size, a->last_scaled_size)) != 0) {
646 print_size = 1;
647 if (strlcpy(a->last_scaled_size, scaled_size,
648 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
649 return got_error(GOT_ERR_NO_SPACE);
651 if (nobj_indexed > 0) {
652 p_indexed = (nobj_indexed * 100) / nobj_total;
653 if (p_indexed != a->last_p_indexed) {
654 a->last_p_indexed = p_indexed;
655 print_indexed = 1;
656 print_size = 1;
659 if (nobj_resolved > 0) {
660 p_resolved = (nobj_resolved * 100) /
661 (nobj_total - nobj_loose);
662 if (p_resolved != a->last_p_resolved) {
663 a->last_p_resolved = p_resolved;
664 print_resolved = 1;
665 print_indexed = 1;
666 print_size = 1;
671 if (print_size || print_indexed || print_resolved)
672 printf("\r");
673 if (print_size)
674 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
675 if (print_indexed)
676 printf("; indexing %d%%", p_indexed);
677 if (print_resolved)
678 printf("; resolving deltas %d%%", p_resolved);
679 if (print_size || print_indexed || print_resolved)
680 fflush(stdout);
682 return NULL;
685 static const struct got_error *
686 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
687 const char *refname, struct got_repository *repo)
689 const struct got_error *err;
690 struct got_reference *ref;
692 *new = NULL;
694 err = got_ref_open(&ref, repo, refname, 0);
695 if (err) {
696 if (err->code != GOT_ERR_NOT_REF)
697 return err;
699 /* Treat argument as a reference prefix. */
700 err = got_ref_list(refs, repo, refname,
701 got_ref_cmp_by_name, NULL);
702 } else {
703 err = got_reflist_insert(new, refs, ref,
704 got_ref_cmp_by_name, NULL);
705 if (err || *new == NULL /* duplicate */)
706 got_ref_close(ref);
709 return err;
712 static const struct got_error *
713 cmd_pack(int argc, char *argv[])
715 const struct got_error *error = NULL;
716 char *repo_path = NULL;
717 struct got_repository *repo = NULL;
718 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
719 struct got_object_id *pack_hash = NULL;
720 char *id_str = NULL;
721 struct got_pack_progress_arg ppa;
722 FILE *packfile = NULL;
723 struct got_pathlist_head exclude_args;
724 struct got_pathlist_entry *pe;
725 struct got_reflist_head exclude_refs;
726 struct got_reflist_head include_refs;
727 struct got_reflist_entry *re, *new;
728 int *pack_fds = NULL;
730 TAILQ_INIT(&exclude_args);
731 TAILQ_INIT(&exclude_refs);
732 TAILQ_INIT(&include_refs);
734 #ifndef PROFILE
735 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
736 NULL) == -1)
737 err(1, "pledge");
738 #endif
740 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
741 switch (ch) {
742 case 'a':
743 loose_obj_only = 0;
744 break;
745 case 'D':
746 force_refdelta = 1;
747 break;
748 case 'q':
749 verbosity = -1;
750 break;
751 case 'r':
752 repo_path = realpath(optarg, NULL);
753 if (repo_path == NULL)
754 return got_error_from_errno2("realpath",
755 optarg);
756 got_path_strip_trailing_slashes(repo_path);
757 break;
758 case 'x':
759 got_path_strip_trailing_slashes(optarg);
760 error = got_pathlist_append(&exclude_args,
761 optarg, NULL);
762 if (error)
763 return error;
764 break;
765 default:
766 usage_pack();
767 /* NOTREACHED */
771 argc -= optind;
772 argv += optind;
774 if (repo_path == NULL) {
775 error = get_repo_path(&repo_path);
776 if (error)
777 goto done;
779 error = got_repo_pack_fds_open(&pack_fds);
780 if (error != NULL)
781 goto done;
782 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
783 if (error)
784 goto done;
786 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
787 if (error)
788 goto done;
790 TAILQ_FOREACH(pe, &exclude_args, entry) {
791 const char *refname = pe->path;
792 error = add_ref(&new, &exclude_refs, refname, repo);
793 if (error)
794 goto done;
797 if (argc == 0) {
798 error = got_ref_list(&include_refs, repo, "",
799 got_ref_cmp_by_name, NULL);
800 if (error)
801 goto done;
802 } else {
803 for (i = 0; i < argc; i++) {
804 const char *refname;
805 got_path_strip_trailing_slashes(argv[i]);
806 refname = argv[i];
807 error = add_ref(&new, &include_refs, refname, repo);
808 if (error)
809 goto done;
813 /* Ignore references in the refs/got/ namespace. */
814 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
815 const char *refname = got_ref_get_name(re->ref);
816 if (strncmp("refs/got/", refname, 9) != 0)
817 continue;
818 TAILQ_REMOVE(&include_refs, re, entry);
819 got_ref_close(re->ref);
820 free(re);
823 memset(&ppa, 0, sizeof(ppa));
824 ppa.out = stdout;
825 ppa.last_scaled_size[0] = '\0';
826 ppa.last_p_indexed = -1;
827 ppa.last_p_resolved = -1;
828 ppa.verbosity = verbosity;
830 error = got_repo_pack_objects(&packfile, &pack_hash,
831 &include_refs, &exclude_refs, repo, loose_obj_only,
832 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
833 if (error) {
834 if (ppa.printed_something)
835 printf("\n");
836 goto done;
839 error = got_object_id_str(&id_str, pack_hash);
840 if (error)
841 goto done;
842 if (verbosity >= 0)
843 printf("\nWrote %s.pack\n", id_str);
845 error = got_repo_index_pack(packfile, pack_hash, repo,
846 pack_index_progress, &ppa, check_cancelled, NULL);
847 if (error)
848 goto done;
849 if (verbosity >= 0)
850 printf("\nIndexed %s.pack\n", id_str);
851 done:
852 if (repo)
853 got_repo_close(repo);
854 if (pack_fds) {
855 const struct got_error *pack_err =
856 got_repo_pack_fds_close(pack_fds);
857 if (error == NULL)
858 error = pack_err;
860 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
861 got_ref_list_free(&exclude_refs);
862 got_ref_list_free(&include_refs);
863 free(id_str);
864 free(pack_hash);
865 free(repo_path);
866 return error;
869 __dead static void
870 usage_indexpack(void)
872 fprintf(stderr, "usage: %s indexpack packfile-path\n",
873 getprogname());
874 exit(1);
877 static const struct got_error *
878 cmd_indexpack(int argc, char *argv[])
880 const struct got_error *error = NULL;
881 struct got_repository *repo = NULL;
882 int ch;
883 struct got_object_id *pack_hash = NULL;
884 char *packfile_path = NULL;
885 char *id_str = NULL;
886 struct got_pack_progress_arg ppa;
887 FILE *packfile = NULL;
888 int *pack_fds = NULL;
890 while ((ch = getopt(argc, argv, "")) != -1) {
891 switch (ch) {
892 default:
893 usage_indexpack();
894 /* NOTREACHED */
898 argc -= optind;
899 argv += optind;
901 if (argc != 1)
902 usage_indexpack();
904 packfile_path = realpath(argv[0], NULL);
905 if (packfile_path == NULL)
906 return got_error_from_errno2("realpath", argv[0]);
908 #ifndef PROFILE
909 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
910 NULL) == -1)
911 err(1, "pledge");
912 #endif
914 error = got_repo_pack_fds_open(&pack_fds);
915 if (error != NULL)
916 goto done;
917 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
918 if (error)
919 goto done;
921 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
922 if (error)
923 goto done;
925 memset(&ppa, 0, sizeof(ppa));
926 ppa.out = stdout;
927 ppa.last_scaled_size[0] = '\0';
928 ppa.last_p_indexed = -1;
929 ppa.last_p_resolved = -1;
931 error = got_repo_find_pack(&packfile, &pack_hash, repo,
932 packfile_path);
933 if (error)
934 goto done;
936 error = got_object_id_str(&id_str, pack_hash);
937 if (error)
938 goto done;
940 error = got_repo_index_pack(packfile, pack_hash, repo,
941 pack_index_progress, &ppa, check_cancelled, NULL);
942 if (error)
943 goto done;
944 printf("\nIndexed %s.pack\n", id_str);
945 done:
946 if (repo)
947 got_repo_close(repo);
948 if (pack_fds) {
949 const struct got_error *pack_err =
950 got_repo_pack_fds_close(pack_fds);
951 if (error == NULL)
952 error = pack_err;
954 free(id_str);
955 free(pack_hash);
956 return error;
959 __dead static void
960 usage_listpack(void)
962 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
963 getprogname());
964 exit(1);
967 struct gotadmin_list_pack_cb_args {
968 int nblobs;
969 int ntrees;
970 int ncommits;
971 int ntags;
972 int noffdeltas;
973 int nrefdeltas;
974 int human_readable;
977 static const struct got_error *
978 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
979 off_t size, off_t base_offset, struct got_object_id *base_id)
981 const struct got_error *err;
982 struct gotadmin_list_pack_cb_args *a = arg;
983 char *id_str, *delta_str = NULL, *base_id_str = NULL;
984 const char *type_str;
986 err = got_object_id_str(&id_str, id);
987 if (err)
988 return err;
990 switch (type) {
991 case GOT_OBJ_TYPE_BLOB:
992 type_str = GOT_OBJ_LABEL_BLOB;
993 a->nblobs++;
994 break;
995 case GOT_OBJ_TYPE_TREE:
996 type_str = GOT_OBJ_LABEL_TREE;
997 a->ntrees++;
998 break;
999 case GOT_OBJ_TYPE_COMMIT:
1000 type_str = GOT_OBJ_LABEL_COMMIT;
1001 a->ncommits++;
1002 break;
1003 case GOT_OBJ_TYPE_TAG:
1004 type_str = GOT_OBJ_LABEL_TAG;
1005 a->ntags++;
1006 break;
1007 case GOT_OBJ_TYPE_OFFSET_DELTA:
1008 type_str = "offset-delta";
1009 if (asprintf(&delta_str, " base-offset %lld",
1010 (long long)base_offset) == -1) {
1011 err = got_error_from_errno("asprintf");
1012 goto done;
1014 a->noffdeltas++;
1015 break;
1016 case GOT_OBJ_TYPE_REF_DELTA:
1017 type_str = "ref-delta";
1018 err = got_object_id_str(&base_id_str, base_id);
1019 if (err)
1020 goto done;
1021 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1022 err = got_error_from_errno("asprintf");
1023 goto done;
1025 a->nrefdeltas++;
1026 break;
1027 default:
1028 err = got_error(GOT_ERR_OBJ_TYPE);
1029 goto done;
1031 if (a->human_readable) {
1032 char scaled[FMT_SCALED_STRSIZE];
1033 char *s;;
1034 if (fmt_scaled(size, scaled) == -1) {
1035 err = got_error_from_errno("fmt_scaled");
1036 goto done;
1038 s = scaled;
1039 while (isspace((unsigned char)*s))
1040 s++;
1041 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1042 (long long)offset, s, delta_str ? delta_str : "");
1043 } else {
1044 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1045 (long long)offset, (long long)size,
1046 delta_str ? delta_str : "");
1048 done:
1049 free(id_str);
1050 free(base_id_str);
1051 free(delta_str);
1052 return err;
1055 static const struct got_error *
1056 cmd_listpack(int argc, char *argv[])
1058 const struct got_error *error = NULL;
1059 struct got_repository *repo = NULL;
1060 int ch;
1061 struct got_object_id *pack_hash = NULL;
1062 char *packfile_path = NULL;
1063 char *id_str = NULL;
1064 struct gotadmin_list_pack_cb_args lpa;
1065 FILE *packfile = NULL;
1066 int show_stats = 0, human_readable = 0;
1067 int *pack_fds = NULL;
1069 #ifndef PROFILE
1070 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1071 NULL) == -1)
1072 err(1, "pledge");
1073 #endif
1075 while ((ch = getopt(argc, argv, "hs")) != -1) {
1076 switch (ch) {
1077 case 'h':
1078 human_readable = 1;
1079 break;
1080 case 's':
1081 show_stats = 1;
1082 break;
1083 default:
1084 usage_listpack();
1085 /* NOTREACHED */
1089 argc -= optind;
1090 argv += optind;
1092 if (argc != 1)
1093 usage_listpack();
1094 packfile_path = realpath(argv[0], NULL);
1095 if (packfile_path == NULL)
1096 return got_error_from_errno2("realpath", argv[0]);
1098 error = got_repo_pack_fds_open(&pack_fds);
1099 if (error != NULL)
1100 goto done;
1101 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1102 if (error)
1103 goto done;
1104 #ifndef PROFILE
1105 /* Remove "cpath" promise. */
1106 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1107 NULL) == -1)
1108 err(1, "pledge");
1109 #endif
1110 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1111 if (error)
1112 goto done;
1114 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1115 packfile_path);
1116 if (error)
1117 goto done;
1118 error = got_object_id_str(&id_str, pack_hash);
1119 if (error)
1120 goto done;
1122 memset(&lpa, 0, sizeof(lpa));
1123 lpa.human_readable = human_readable;
1124 error = got_repo_list_pack(packfile, pack_hash, repo,
1125 list_pack_cb, &lpa, check_cancelled, NULL);
1126 if (error)
1127 goto done;
1128 if (show_stats) {
1129 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1130 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1131 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1132 lpa.noffdeltas + lpa.nrefdeltas,
1133 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1134 lpa.noffdeltas, lpa.nrefdeltas);
1136 done:
1137 if (repo)
1138 got_repo_close(repo);
1139 if (pack_fds) {
1140 const struct got_error *pack_err =
1141 got_repo_pack_fds_close(pack_fds);
1142 if (error == NULL)
1143 error = pack_err;
1145 free(id_str);
1146 free(pack_hash);
1147 free(packfile_path);
1148 return error;
1151 __dead static void
1152 usage_cleanup(void)
1154 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1155 getprogname());
1156 exit(1);
1159 struct got_cleanup_progress_arg {
1160 int last_nloose;
1161 int last_ncommits;
1162 int last_npurged;
1163 int last_nredundant;
1164 int verbosity;
1165 int printed_something;
1166 int dry_run;
1169 static const struct got_error *
1170 cleanup_progress(void *arg, int ncommits, int nloose, int npurged,
1171 int nredundant)
1173 struct got_cleanup_progress_arg *a = arg;
1174 int print_loose = 0, print_commits = 0, print_purged = 0;
1175 int print_redundant = 0;
1177 if (a->last_ncommits != ncommits) {
1178 print_commits = 1;
1179 a->last_ncommits = ncommits;
1181 if (a->last_nloose != nloose) {
1182 print_commits = 1;
1183 print_loose = 1;
1184 a->last_nloose = nloose;
1186 if (a->last_npurged != npurged) {
1187 print_commits = 1;
1188 print_loose = 1;
1189 print_purged = 1;
1190 a->last_npurged = npurged;
1192 if (a->last_nredundant != nredundant) {
1193 print_commits = 1;
1194 print_loose = 1;
1195 print_purged = 1;
1196 print_redundant = 1;
1197 a->last_nredundant = nredundant;
1200 if (a->verbosity < 0)
1201 return NULL;
1203 if (print_loose || print_commits || print_purged || print_redundant)
1204 printf("\r");
1205 if (print_commits)
1206 printf("%d commit%s scanned", ncommits,
1207 ncommits == 1 ? "" : "s");
1208 if (print_loose)
1209 printf("; %d loose object%s", nloose, nloose == 1 ? "" : "s");
1210 if (print_purged || print_redundant) {
1211 if (a->dry_run) {
1212 printf("; could purge %d object%s", npurged,
1213 npurged == 1 ? "" : "s");
1214 } else {
1215 printf("; purged %d object%s", npurged,
1216 npurged == 1 ? "" : "s");
1219 if (print_redundant) {
1220 if (a->dry_run) {
1221 printf(", %d pack file%s", nredundant,
1222 nredundant == 1 ? "" : "s");
1223 } else {
1224 printf(", %d pack file%s", nredundant,
1225 nredundant == 1 ? "" : "s");
1228 if (print_loose || print_commits || print_purged || print_redundant) {
1229 a->printed_something = 1;
1230 fflush(stdout);
1232 return NULL;
1235 struct got_lonely_packidx_progress_arg {
1236 int verbosity;
1237 int printed_something;
1238 int dry_run;
1241 static const struct got_error *
1242 lonely_packidx_progress(void *arg, const char *path)
1244 struct got_lonely_packidx_progress_arg *a = arg;
1246 if (a->verbosity < 0)
1247 return NULL;
1249 if (a->dry_run)
1250 printf("%s could be removed\n", path);
1251 else
1252 printf("%s removed\n", path);
1254 a->printed_something = 1;
1255 return NULL;
1258 static const struct got_error *
1259 cmd_cleanup(int argc, char *argv[])
1261 const struct got_error *error = NULL;
1262 char *repo_path = NULL;
1263 struct got_repository *repo = NULL;
1264 int ch, dry_run = 0, verbosity = 0;
1265 int ncommits = 0, nloose = 0, npacked = 0;
1266 int remove_lonely_packidx = 0, ignore_mtime = 0;
1267 struct got_cleanup_progress_arg cpa;
1268 struct got_lonely_packidx_progress_arg lpa;
1269 off_t loose_before, loose_after;
1270 off_t pack_before, pack_after;
1271 off_t total_size;
1272 char loose_before_scaled[FMT_SCALED_STRSIZE];
1273 char loose_after_scaled[FMT_SCALED_STRSIZE];
1274 char pack_before_scaled[FMT_SCALED_STRSIZE];
1275 char pack_after_scaled[FMT_SCALED_STRSIZE];
1276 char total_size_scaled[FMT_SCALED_STRSIZE];
1277 int *pack_fds = NULL;
1279 #ifndef PROFILE
1280 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1281 NULL) == -1)
1282 err(1, "pledge");
1283 #endif
1285 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1286 switch (ch) {
1287 case 'a':
1288 ignore_mtime = 1;
1289 break;
1290 case 'n':
1291 dry_run = 1;
1292 break;
1293 case 'p':
1294 remove_lonely_packidx = 1;
1295 break;
1296 case 'q':
1297 verbosity = -1;
1298 break;
1299 case 'r':
1300 repo_path = realpath(optarg, NULL);
1301 if (repo_path == NULL)
1302 return got_error_from_errno2("realpath",
1303 optarg);
1304 got_path_strip_trailing_slashes(repo_path);
1305 break;
1306 default:
1307 usage_cleanup();
1308 /* NOTREACHED */
1312 argc -= optind;
1313 argv += optind;
1315 if (repo_path == NULL) {
1316 error = get_repo_path(&repo_path);
1317 if (error)
1318 goto done;
1320 error = got_repo_pack_fds_open(&pack_fds);
1321 if (error != NULL)
1322 goto done;
1323 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1324 if (error)
1325 goto done;
1327 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1328 if (error)
1329 goto done;
1331 if (got_repo_has_extension(repo, "preciousObjects")) {
1332 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1333 "the preciousObjects Git extension is enabled; "
1334 "this implies that objects must not be deleted");
1335 goto done;
1338 if (remove_lonely_packidx) {
1339 memset(&lpa, 0, sizeof(lpa));
1340 lpa.dry_run = dry_run;
1341 lpa.verbosity = verbosity;
1342 error = got_repo_remove_lonely_packidx(repo, dry_run,
1343 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1344 goto done;
1347 memset(&cpa, 0, sizeof(cpa));
1348 cpa.last_nloose = -1;
1349 cpa.last_npurged = -1;
1350 cpa.last_nredundant = -1;
1351 cpa.dry_run = dry_run;
1352 cpa.verbosity = verbosity;
1354 error = got_repo_cleanup(repo, &loose_before, &loose_after,
1355 &pack_before, &pack_after, &ncommits, &nloose, &npacked,
1356 dry_run, ignore_mtime, cleanup_progress, &cpa,
1357 check_cancelled, NULL);
1358 if (cpa.printed_something)
1359 printf("\n");
1360 if (error)
1361 goto done;
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");
1368 goto done;
1370 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1371 error = got_error_from_errno("fmt_scaled");
1372 goto done;
1374 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1375 error = got_error_from_errno("fmt_scaled");
1376 goto done;
1378 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1379 error = got_error_from_errno("fmt_scaled");
1380 goto done;
1382 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1383 error = got_error_from_errno("fmt_scaled");
1384 goto done;
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);
1391 if (dry_run) {
1392 printf("disk space which would be freed: %s\n",
1393 total_size_scaled);
1394 } else
1395 printf("disk space freed: %s\n", total_size_scaled);
1396 printf("loose objects also found in pack files: %d\n", npacked);
1399 done:
1400 if (repo)
1401 got_repo_close(repo);
1402 if (pack_fds) {
1403 const struct got_error *pack_err =
1404 got_repo_pack_fds_close(pack_fds);
1405 if (error == NULL)
1406 error = pack_err;
1408 free(repo_path);
1409 return error;
1412 __dead static void
1413 usage_dump(void)
1415 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1416 "[-x reference] [reference]...\n", getprogname());
1417 exit(1);
1420 static const struct got_error *
1421 cmd_dump(int argc, char *argv[])
1423 const struct got_error *error = NULL;
1424 struct got_pack_progress_arg ppa;
1425 struct got_repository *repo = NULL;
1426 struct got_pathlist_head exclude_args;
1427 struct got_pathlist_entry *pe;
1428 struct got_reflist_head exclude_refs;
1429 struct got_reflist_head include_refs;
1430 struct got_reflist_entry *re, *new;
1431 const char *refname;
1432 char *repo_path = NULL;
1433 int *pack_fds = NULL;
1434 int verbosity = 0;
1435 int i, ch;
1437 TAILQ_INIT(&exclude_args);
1438 TAILQ_INIT(&exclude_refs);
1439 TAILQ_INIT(&include_refs);
1441 #ifndef PROFILE
1442 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1443 NULL) == -1)
1444 err(1, "pledge");
1445 #endif
1447 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1448 switch (ch) {
1449 case 'q':
1450 verbosity = -1;
1451 break;
1452 case 'r':
1453 repo_path = realpath(optarg, NULL);
1454 if (repo_path == NULL)
1455 return got_error_from_errno2("realpath",
1456 optarg);
1457 got_path_strip_trailing_slashes(repo_path);
1458 break;
1459 case 'x':
1460 error = got_pathlist_append(&exclude_args,
1461 optarg, NULL);
1462 if (error)
1463 return error;
1464 break;
1465 default:
1466 usage_dump();
1467 /* NOTREACHED */
1470 argc -= optind;
1471 argv += optind;
1473 if (repo_path == NULL) {
1474 error = get_repo_path(&repo_path);
1475 if (error)
1476 goto done;
1478 error = got_repo_pack_fds_open(&pack_fds);
1479 if (error != NULL)
1480 goto done;
1481 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1482 if (error)
1483 goto done;
1485 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1486 if (error)
1487 goto done;
1489 TAILQ_FOREACH(pe, &exclude_args, entry) {
1490 refname = pe->path;
1491 error = add_ref(&new, &exclude_refs, refname, repo);
1492 if (error)
1493 goto done;
1496 if (argc == 0) {
1497 error = got_ref_list(&include_refs, repo, "",
1498 got_ref_cmp_by_name, NULL);
1499 if (error)
1500 goto done;
1501 } else {
1502 for (i = 0; i < argc; i++) {
1503 got_path_strip_trailing_slashes(argv[i]);
1504 refname = argv[i];
1505 error = add_ref(&new, &include_refs, refname, repo);
1506 if (error)
1507 goto done;
1511 /* Ignore references in the refs/got/ namespace. */
1512 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1513 refname = got_ref_get_name(re->ref);
1514 if (strncmp("refs/got/", refname, 9) != 0)
1515 continue;
1516 TAILQ_REMOVE(&include_refs, re, entry);
1517 got_ref_close(re->ref);
1518 free(re);
1521 memset(&ppa, 0, sizeof(ppa));
1522 ppa.out = stderr;
1523 ppa.verbosity = verbosity;
1525 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1526 repo, pack_progress, &ppa, check_cancelled, NULL);
1527 if (ppa.printed_something)
1528 fprintf(stderr, "\n");
1529 done:
1530 if (repo)
1531 got_repo_close(repo);
1533 if (pack_fds) {
1534 const struct got_error *pack_err;
1536 pack_err = got_repo_pack_fds_close(pack_fds);
1537 if (error == NULL)
1538 error = pack_err;
1541 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1542 got_ref_list_free(&exclude_refs);
1543 got_ref_list_free(&include_refs);
1544 free(repo_path);
1546 return error;
1549 __dead static void
1550 usage_load(void)
1552 fprintf(stderr, "usage: %s load [-nq] [-l bundle-file] "
1553 "[-r repository-path] [reference ...]\n",
1554 getprogname());
1555 exit(1);
1558 static const struct got_error *
1559 load_progress(void *arg, off_t packfile_size, int nobj_total,
1560 int nobj_indexed, int nobj_loose, int nobj_resolved)
1562 return pack_index_progress(arg, packfile_size, nobj_total,
1563 nobj_indexed, nobj_loose, nobj_resolved);
1566 static int
1567 is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1569 struct got_pathlist_entry *pe;
1571 if (TAILQ_EMPTY(wanted))
1572 return 1;
1574 TAILQ_FOREACH(pe, wanted, entry) {
1575 if (strcmp(pe->path, ref) == 0)
1576 return 1;
1579 return 0;
1582 static const struct got_error *
1583 create_ref(const char *refname, struct got_object_id *id,
1584 int verbosity, struct got_repository *repo)
1586 const struct got_error *err = NULL;
1587 struct got_reference *ref;
1588 char *id_str;
1590 err = got_object_id_str(&id_str, id);
1591 if (err)
1592 return err;
1594 err = got_ref_alloc(&ref, refname, id);
1595 if (err)
1596 goto done;
1598 err = got_ref_write(ref, repo);
1599 got_ref_close(ref);
1601 if (err == NULL && verbosity >= 0)
1602 printf("Created reference %s: %s\n", refname, id_str);
1603 done:
1604 free(id_str);
1605 return err;
1608 static const struct got_error *
1609 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1610 int replace_tags, int verbosity, struct got_repository *repo)
1612 const struct got_error *err = NULL;
1613 char *new_id_str = NULL;
1614 struct got_object_id *old_id = NULL;
1616 err = got_object_id_str(&new_id_str, new_id);
1617 if (err)
1618 goto done;
1620 if (!replace_tags &&
1621 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1622 err = got_ref_resolve(&old_id, repo, ref);
1623 if (err)
1624 goto done;
1625 if (got_object_id_cmp(old_id, new_id) == 0)
1626 goto done;
1627 if (verbosity >= 0) {
1628 printf("Rejecting update of existing tag %s: %s\n",
1629 got_ref_get_name(ref), new_id_str);
1631 goto done;
1634 if (got_ref_is_symbolic(ref)) {
1635 if (verbosity >= 0) {
1636 printf("Replacing reference %s: %s\n",
1637 got_ref_get_name(ref),
1638 got_ref_get_symref_target(ref));
1640 err = got_ref_change_symref_to_ref(ref, new_id);
1641 if (err)
1642 goto done;
1643 err = got_ref_write(ref, repo);
1644 if (err)
1645 goto done;
1646 } else {
1647 err = got_ref_resolve(&old_id, repo, ref);
1648 if (err)
1649 goto done;
1650 if (got_object_id_cmp(old_id, new_id) == 0)
1651 goto done;
1653 err = got_ref_change_ref(ref, new_id);
1654 if (err)
1655 goto done;
1656 err = got_ref_write(ref, repo);
1657 if (err)
1658 goto done;
1661 if (verbosity >= 0)
1662 printf("Updated %s: %s\n", got_ref_get_name(ref),
1663 new_id_str);
1664 done:
1665 free(old_id);
1666 free(new_id_str);
1667 return err;
1670 static const struct got_error *
1671 cmd_load(int argc, char *argv[])
1673 const struct got_error *error = NULL;
1674 struct got_repository *repo = NULL;
1675 struct got_pathlist_head include_args;
1676 struct got_pathlist_head available_refs;
1677 struct got_pathlist_entry *pe;
1678 struct got_pack_progress_arg ppa;
1679 FILE *in = stdin;
1680 int *pack_fds = NULL;
1681 char *repo_path = NULL;
1682 int list_refs_only = 0;
1683 int noop = 0;
1684 int verbosity = 0;
1685 int ch, i;
1687 TAILQ_INIT(&include_args);
1688 TAILQ_INIT(&available_refs);
1690 #ifndef PROFILE
1691 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1692 "sendfd unveil", NULL) == -1)
1693 err(1, "pledge");
1694 #endif
1696 while ((ch = getopt(argc, argv, "l:nqr:")) != -1) {
1697 switch (ch) {
1698 case 'l':
1699 list_refs_only = 1;
1700 in = fopen(optarg, "re");
1701 if (in == NULL)
1702 return got_error_from_errno2("open", optarg);
1703 break;
1704 case 'n':
1705 noop = 1;
1706 break;
1707 case 'q':
1708 verbosity = -1;
1709 break;
1710 case 'r':
1711 repo_path = realpath(optarg, NULL);
1712 if (repo_path == NULL)
1713 return got_error_from_errno2("realpath",
1714 optarg);
1715 got_path_strip_trailing_slashes(repo_path);
1716 break;
1717 default:
1718 usage_load();
1719 /* NOTREACHED */
1722 argc -= optind;
1723 argv += optind;
1725 if (list_refs_only && argc > 1)
1726 errx(1, "-l and references on the command line are exclusive");
1727 if (list_refs_only && noop)
1728 errx(1, "-n and -l are mutually exclusive");
1730 for (i = 0; i < argc; i++) {
1731 char *refname = argv[i];
1732 got_path_strip_trailing_slashes(refname);
1733 if (!got_ref_name_is_valid(refname))
1734 errx(1, "invalid reference name %s", refname);
1735 error = got_pathlist_append(&include_args, refname, NULL);
1736 if (error)
1737 goto done;
1740 if (repo_path == NULL) {
1741 error = get_repo_path(&repo_path);
1742 if (error)
1743 goto done;
1745 error = got_repo_pack_fds_open(&pack_fds);
1746 if (error != NULL)
1747 goto done;
1748 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1749 if (error)
1750 goto done;
1752 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1753 if (error)
1754 goto done;
1756 memset(&ppa, 0, sizeof(ppa));
1757 ppa.out = stdout;
1758 ppa.verbosity = verbosity;
1760 error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1761 load_progress, &ppa, check_cancelled, NULL);
1762 if (verbosity >= 0 && !list_refs_only)
1763 printf("\n");
1764 if (error)
1765 goto done;
1767 if (list_refs_only) {
1768 TAILQ_FOREACH(pe, &available_refs, entry) {
1769 const char *refname = pe->path;
1770 struct got_object_id *id = pe->data;
1771 char *idstr;
1773 error = got_object_id_str(&idstr, id);
1774 if (error)
1775 goto done;
1777 printf("%s: %s\n", refname, idstr);
1778 free(idstr);
1780 goto done;
1783 if (noop)
1784 goto done;
1786 /* Update references */
1787 TAILQ_FOREACH(pe, &available_refs, entry) {
1788 const struct got_error *unlock_err;
1789 struct got_reference *ref;
1790 const char *refname = pe->path;
1791 struct got_object_id *id = pe->data;
1793 if (!is_wanted_ref(&include_args, pe->path))
1794 continue;
1796 error = got_ref_open(&ref, repo, refname, 1);
1797 if (error) {
1798 if (error->code != GOT_ERR_NOT_REF)
1799 goto done;
1800 error = create_ref(refname, id, verbosity, repo);
1801 if (error)
1802 goto done;
1803 } else {
1804 /* XXX: check advances only and add -f to force? */
1805 error = update_ref(ref, id, 1, verbosity, repo);
1806 unlock_err = got_ref_unlock(ref);
1807 if (unlock_err && error == NULL)
1808 error = unlock_err;
1809 got_ref_close(ref);
1810 if (error)
1811 goto done;
1815 done:
1816 if (in != stdin && fclose(in) == EOF && error == NULL)
1817 error = got_error_from_errno("fclose");
1819 if (repo)
1820 got_repo_close(repo);
1822 if (pack_fds) {
1823 const struct got_error *pack_err;
1825 pack_err = got_repo_pack_fds_close(pack_fds);
1826 if (error == NULL)
1827 error = pack_err;
1830 got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1831 got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);
1832 free(repo_path);
1834 return error;