Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "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)
58 {
59 sigint_received = 1;
60 }
62 static void
63 catch_sigpipe(int signo)
64 {
65 sigpipe_received = 1;
66 }
68 static const struct got_error *
69 check_cancelled(void *arg)
70 {
71 if (sigint_received || sigpipe_received)
72 return got_error(GOT_ERR_CANCELLED);
73 return NULL;
74 }
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;
81 };
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, "" },
111 };
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);
126 int
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 }
136 };
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 [-b branch] repository-path\n",
283 getprogname());
284 exit(1);
287 static const struct got_error *
288 cmd_init(int argc, char *argv[])
290 const struct got_error *error = NULL;
291 const char *head_name = NULL;
292 char *repo_path = NULL;
293 int ch;
295 #ifndef PROFILE
296 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
297 err(1, "pledge");
298 #endif
300 while ((ch = getopt(argc, argv, "b:")) != -1) {
301 switch (ch) {
302 case 'b':
303 head_name = optarg;
304 break;
305 default:
306 usage_init();
307 /* NOTREACHED */
311 argc -= optind;
312 argv += optind;
314 if (argc != 1)
315 usage_init();
317 repo_path = strdup(argv[0]);
318 if (repo_path == NULL)
319 return got_error_from_errno("strdup");
321 got_path_strip_trailing_slashes(repo_path);
323 error = got_path_mkdir(repo_path);
324 if (error &&
325 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
326 goto done;
328 error = apply_unveil(repo_path, 0);
329 if (error)
330 goto done;
332 error = got_repo_init(repo_path, head_name);
333 done:
334 free(repo_path);
335 return error;
338 static const struct got_error *
339 cmd_info(int argc, char *argv[])
341 const struct got_error *error = NULL;
342 char *repo_path = NULL;
343 struct got_repository *repo = NULL;
344 const struct got_gotconfig *gotconfig = NULL;
345 int ch, npackfiles, npackedobj, nobj;
346 off_t packsize, loose_size;
347 char scaled[FMT_SCALED_STRSIZE];
348 int *pack_fds = NULL;
350 #ifndef PROFILE
351 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
352 NULL) == -1)
353 err(1, "pledge");
354 #endif
356 while ((ch = getopt(argc, argv, "r:")) != -1) {
357 switch (ch) {
358 case 'r':
359 repo_path = realpath(optarg, NULL);
360 if (repo_path == NULL)
361 return got_error_from_errno2("realpath",
362 optarg);
363 got_path_strip_trailing_slashes(repo_path);
364 break;
365 default:
366 usage_info();
367 /* NOTREACHED */
371 argc -= optind;
372 argv += optind;
374 if (repo_path == NULL) {
375 error = get_repo_path(&repo_path);
376 if (error)
377 goto done;
379 error = got_repo_pack_fds_open(&pack_fds);
380 if (error != NULL)
381 goto done;
382 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
383 if (error)
384 goto done;
385 #ifndef PROFILE
386 /* Remove "cpath" promise. */
387 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
388 NULL) == -1)
389 err(1, "pledge");
390 #endif
391 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
392 if (error)
393 goto done;
395 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
397 gotconfig = got_repo_get_gotconfig(repo);
398 if (gotconfig) {
399 const struct got_remote_repo *remotes;
400 int i, nremotes;
401 if (got_gotconfig_get_author(gotconfig)) {
402 printf("default author: %s\n",
403 got_gotconfig_get_author(gotconfig));
405 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
406 for (i = 0; i < nremotes; i++) {
407 const char *fetch_url = remotes[i].fetch_url;
408 const char *send_url = remotes[i].send_url;
409 if (strcmp(fetch_url, send_url) == 0) {
410 printf("remote \"%s\": %s\n", remotes[i].name,
411 remotes[i].fetch_url);
412 } else {
413 printf("remote \"%s\" (fetch): %s\n",
414 remotes[i].name, remotes[i].fetch_url);
415 printf("remote \"%s\" (send): %s\n",
416 remotes[i].name, remotes[i].send_url);
421 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
422 &packsize, repo);
423 if (error)
424 goto done;
425 printf("pack files: %d\n", npackfiles);
426 if (npackfiles > 0) {
427 if (fmt_scaled(packsize, scaled) == -1) {
428 error = got_error_from_errno("fmt_scaled");
429 goto done;
431 printf("packed objects: %d\n", npackedobj);
432 printf("packed total size: %s\n", scaled);
435 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
436 if (error)
437 goto done;
438 printf("loose objects: %d\n", nobj);
439 if (nobj > 0) {
440 if (fmt_scaled(loose_size, scaled) == -1) {
441 error = got_error_from_errno("fmt_scaled");
442 goto done;
444 printf("loose total size: %s\n", scaled);
446 done:
447 if (repo)
448 got_repo_close(repo);
449 if (pack_fds) {
450 const struct got_error *pack_err =
451 got_repo_pack_fds_close(pack_fds);
452 if (error == NULL)
453 error = pack_err;
456 free(repo_path);
457 return error;
460 __dead static void
461 usage_pack(void)
463 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
464 "[-x reference] [reference ...]\n", getprogname());
465 exit(1);
468 struct got_pack_progress_arg {
469 FILE *out;
470 char last_scaled_size[FMT_SCALED_STRSIZE];
471 int last_ncolored;
472 int last_nfound;
473 int last_ntrees;
474 int loading_done;
475 int last_ncommits;
476 int last_nobj_total;
477 int last_p_deltify;
478 int last_p_written;
479 int last_p_indexed;
480 int last_p_resolved;
481 int verbosity;
482 int printed_something;
483 };
485 static void
486 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
487 int ncolored, int nfound, int ntrees)
489 if (print_colored) {
490 fprintf(out, "%d commit%s colored", ncolored,
491 ncolored == 1 ? "" : "s");
493 if (print_found) {
494 fprintf(out, "%s%d object%s found",
495 ncolored > 0 ? "; " : "",
496 nfound, nfound == 1 ? "" : "s");
498 if (print_trees) {
499 fprintf(out, "; %d tree%s scanned", ntrees,
500 ntrees == 1 ? "" : "s");
504 static const struct got_error *
505 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
506 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
507 int nobj_written)
509 struct got_pack_progress_arg *a = arg;
510 char scaled_size[FMT_SCALED_STRSIZE];
511 int p_deltify, p_written;
512 int print_colored = 0, print_found = 0, print_trees = 0;
513 int print_searching = 0, print_total = 0;
514 int print_deltify = 0, print_written = 0;
516 if (a->verbosity < 0)
517 return NULL;
519 if (a->last_ncolored != ncolored) {
520 print_colored = 1;
521 a->last_ncolored = ncolored;
524 if (a->last_nfound != nfound) {
525 print_colored = 1;
526 print_found = 1;
527 a->last_nfound = nfound;
530 if (a->last_ntrees != ntrees) {
531 print_colored = 1;
532 print_found = 1;
533 print_trees = 1;
534 a->last_ntrees = ntrees;
537 if ((print_colored || print_found || print_trees) &&
538 !a->loading_done) {
539 fprintf(a->out, "\r");
540 print_load_info(a->out, print_colored, print_found,
541 print_trees, ncolored, nfound, ntrees);
542 a->printed_something = 1;
543 fflush(a->out);
544 return NULL;
545 } else if (!a->loading_done) {
546 fprintf(a->out, "\r");
547 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
548 fprintf(a->out, "\n");
549 a->loading_done = 1;
552 if (fmt_scaled(packfile_size, scaled_size) == -1)
553 return got_error_from_errno("fmt_scaled");
555 if (a->last_ncommits != ncommits) {
556 print_searching = 1;
557 a->last_ncommits = ncommits;
560 if (a->last_nobj_total != nobj_total) {
561 print_searching = 1;
562 print_total = 1;
563 a->last_nobj_total = nobj_total;
566 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
567 strcmp(scaled_size, a->last_scaled_size)) != 0) {
568 if (strlcpy(a->last_scaled_size, scaled_size,
569 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
570 return got_error(GOT_ERR_NO_SPACE);
573 if (nobj_deltify > 0 || nobj_written > 0) {
574 if (nobj_deltify > 0) {
575 p_deltify = (nobj_deltify * 100) / nobj_total;
576 if (p_deltify != a->last_p_deltify) {
577 a->last_p_deltify = p_deltify;
578 print_searching = 1;
579 print_total = 1;
580 print_deltify = 1;
583 if (nobj_written > 0) {
584 p_written = (nobj_written * 100) / nobj_total;
585 if (p_written != a->last_p_written) {
586 a->last_p_written = p_written;
587 print_searching = 1;
588 print_total = 1;
589 print_deltify = 1;
590 print_written = 1;
595 if (print_searching || print_total || print_deltify || print_written)
596 fprintf(a->out, "\r");
597 if (print_searching)
598 fprintf(a->out, "packing %d reference%s", ncommits,
599 ncommits == 1 ? "" : "s");
600 if (print_total)
601 fprintf(a->out, "; %d object%s", nobj_total,
602 nobj_total == 1 ? "" : "s");
603 if (print_deltify)
604 fprintf(a->out, "; deltify: %d%%", p_deltify);
605 if (print_written)
606 fprintf(a->out, "; writing pack: %*s %d%%",
607 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
608 if (print_searching || print_total || print_deltify ||
609 print_written) {
610 a->printed_something = 1;
611 fflush(a->out);
613 return NULL;
616 static const struct got_error *
617 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
618 int nobj_indexed, int nobj_loose, int nobj_resolved)
620 struct got_pack_progress_arg *a = arg;
621 char scaled_size[FMT_SCALED_STRSIZE];
622 int p_indexed, p_resolved;
623 int print_size = 0, print_indexed = 0, print_resolved = 0;
625 if (a->verbosity < 0)
626 return NULL;
628 if (packfile_size > 0 || nobj_indexed > 0) {
629 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
630 (a->last_scaled_size[0] == '\0' ||
631 strcmp(scaled_size, a->last_scaled_size)) != 0) {
632 print_size = 1;
633 if (strlcpy(a->last_scaled_size, scaled_size,
634 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
635 return got_error(GOT_ERR_NO_SPACE);
637 if (nobj_indexed > 0) {
638 p_indexed = (nobj_indexed * 100) / nobj_total;
639 if (p_indexed != a->last_p_indexed) {
640 a->last_p_indexed = p_indexed;
641 print_indexed = 1;
642 print_size = 1;
645 if (nobj_resolved > 0) {
646 p_resolved = (nobj_resolved * 100) /
647 (nobj_total - nobj_loose);
648 if (p_resolved != a->last_p_resolved) {
649 a->last_p_resolved = p_resolved;
650 print_resolved = 1;
651 print_indexed = 1;
652 print_size = 1;
657 if (print_size || print_indexed || print_resolved)
658 printf("\r");
659 if (print_size)
660 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
661 if (print_indexed)
662 printf("; indexing %d%%", p_indexed);
663 if (print_resolved)
664 printf("; resolving deltas %d%%", p_resolved);
665 if (print_size || print_indexed || print_resolved)
666 fflush(stdout);
668 return NULL;
671 static const struct got_error *
672 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
673 const char *refname, struct got_repository *repo)
675 const struct got_error *err;
676 struct got_reference *ref;
678 *new = NULL;
680 err = got_ref_open(&ref, repo, refname, 0);
681 if (err) {
682 if (err->code != GOT_ERR_NOT_REF)
683 return err;
685 /* Treat argument as a reference prefix. */
686 err = got_ref_list(refs, repo, refname,
687 got_ref_cmp_by_name, NULL);
688 } else {
689 err = got_reflist_insert(new, refs, ref,
690 got_ref_cmp_by_name, NULL);
691 if (err || *new == NULL /* duplicate */)
692 got_ref_close(ref);
695 return err;
698 static const struct got_error *
699 cmd_pack(int argc, char *argv[])
701 const struct got_error *error = NULL;
702 char *repo_path = NULL;
703 struct got_repository *repo = NULL;
704 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
705 struct got_object_id *pack_hash = NULL;
706 char *id_str = NULL;
707 struct got_pack_progress_arg ppa;
708 FILE *packfile = NULL;
709 struct got_pathlist_head exclude_args;
710 struct got_pathlist_entry *pe;
711 struct got_reflist_head exclude_refs;
712 struct got_reflist_head include_refs;
713 struct got_reflist_entry *re, *new;
714 int *pack_fds = NULL;
716 TAILQ_INIT(&exclude_args);
717 TAILQ_INIT(&exclude_refs);
718 TAILQ_INIT(&include_refs);
720 #ifndef PROFILE
721 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
722 NULL) == -1)
723 err(1, "pledge");
724 #endif
726 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
727 switch (ch) {
728 case 'a':
729 loose_obj_only = 0;
730 break;
731 case 'D':
732 force_refdelta = 1;
733 break;
734 case 'q':
735 verbosity = -1;
736 break;
737 case 'r':
738 repo_path = realpath(optarg, NULL);
739 if (repo_path == NULL)
740 return got_error_from_errno2("realpath",
741 optarg);
742 got_path_strip_trailing_slashes(repo_path);
743 break;
744 case 'x':
745 got_path_strip_trailing_slashes(optarg);
746 error = got_pathlist_append(&exclude_args,
747 optarg, NULL);
748 if (error)
749 return error;
750 break;
751 default:
752 usage_pack();
753 /* NOTREACHED */
757 argc -= optind;
758 argv += optind;
760 if (repo_path == NULL) {
761 error = get_repo_path(&repo_path);
762 if (error)
763 goto done;
765 error = got_repo_pack_fds_open(&pack_fds);
766 if (error != NULL)
767 goto done;
768 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
769 if (error)
770 goto done;
772 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
773 if (error)
774 goto done;
776 TAILQ_FOREACH(pe, &exclude_args, entry) {
777 const char *refname = pe->path;
778 error = add_ref(&new, &exclude_refs, refname, repo);
779 if (error)
780 goto done;
783 if (argc == 0) {
784 error = got_ref_list(&include_refs, repo, "",
785 got_ref_cmp_by_name, NULL);
786 if (error)
787 goto done;
788 } else {
789 for (i = 0; i < argc; i++) {
790 const char *refname;
791 got_path_strip_trailing_slashes(argv[i]);
792 refname = argv[i];
793 error = add_ref(&new, &include_refs, refname, repo);
794 if (error)
795 goto done;
799 /* Ignore references in the refs/got/ namespace. */
800 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
801 const char *refname = got_ref_get_name(re->ref);
802 if (strncmp("refs/got/", refname, 9) != 0)
803 continue;
804 TAILQ_REMOVE(&include_refs, re, entry);
805 got_ref_close(re->ref);
806 free(re);
809 memset(&ppa, 0, sizeof(ppa));
810 ppa.out = stdout;
811 ppa.last_scaled_size[0] = '\0';
812 ppa.last_p_indexed = -1;
813 ppa.last_p_resolved = -1;
814 ppa.verbosity = verbosity;
816 error = got_repo_pack_objects(&packfile, &pack_hash,
817 &include_refs, &exclude_refs, repo, loose_obj_only,
818 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
819 if (error) {
820 if (ppa.printed_something)
821 printf("\n");
822 goto done;
825 error = got_object_id_str(&id_str, pack_hash);
826 if (error)
827 goto done;
828 if (verbosity >= 0)
829 printf("\nWrote %s.pack\n", id_str);
831 error = got_repo_index_pack(packfile, pack_hash, repo,
832 pack_index_progress, &ppa, check_cancelled, NULL);
833 if (error)
834 goto done;
835 if (verbosity >= 0)
836 printf("\nIndexed %s.pack\n", id_str);
837 done:
838 if (repo)
839 got_repo_close(repo);
840 if (pack_fds) {
841 const struct got_error *pack_err =
842 got_repo_pack_fds_close(pack_fds);
843 if (error == NULL)
844 error = pack_err;
846 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
847 got_ref_list_free(&exclude_refs);
848 got_ref_list_free(&include_refs);
849 free(id_str);
850 free(pack_hash);
851 free(repo_path);
852 return error;
855 __dead static void
856 usage_indexpack(void)
858 fprintf(stderr, "usage: %s indexpack packfile-path\n",
859 getprogname());
860 exit(1);
863 static const struct got_error *
864 cmd_indexpack(int argc, char *argv[])
866 const struct got_error *error = NULL;
867 struct got_repository *repo = NULL;
868 int ch;
869 struct got_object_id *pack_hash = NULL;
870 char *packfile_path = NULL;
871 char *id_str = NULL;
872 struct got_pack_progress_arg ppa;
873 FILE *packfile = NULL;
874 int *pack_fds = NULL;
876 while ((ch = getopt(argc, argv, "")) != -1) {
877 switch (ch) {
878 default:
879 usage_indexpack();
880 /* NOTREACHED */
884 argc -= optind;
885 argv += optind;
887 if (argc != 1)
888 usage_indexpack();
890 packfile_path = realpath(argv[0], NULL);
891 if (packfile_path == NULL)
892 return got_error_from_errno2("realpath", argv[0]);
894 #ifndef PROFILE
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
896 NULL) == -1)
897 err(1, "pledge");
898 #endif
900 error = got_repo_pack_fds_open(&pack_fds);
901 if (error != NULL)
902 goto done;
903 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
904 if (error)
905 goto done;
907 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
908 if (error)
909 goto done;
911 memset(&ppa, 0, sizeof(ppa));
912 ppa.out = stdout;
913 ppa.last_scaled_size[0] = '\0';
914 ppa.last_p_indexed = -1;
915 ppa.last_p_resolved = -1;
917 error = got_repo_find_pack(&packfile, &pack_hash, repo,
918 packfile_path);
919 if (error)
920 goto done;
922 error = got_object_id_str(&id_str, pack_hash);
923 if (error)
924 goto done;
926 error = got_repo_index_pack(packfile, pack_hash, repo,
927 pack_index_progress, &ppa, check_cancelled, NULL);
928 if (error)
929 goto done;
930 printf("\nIndexed %s.pack\n", id_str);
931 done:
932 if (repo)
933 got_repo_close(repo);
934 if (pack_fds) {
935 const struct got_error *pack_err =
936 got_repo_pack_fds_close(pack_fds);
937 if (error == NULL)
938 error = pack_err;
940 free(id_str);
941 free(pack_hash);
942 return error;
945 __dead static void
946 usage_listpack(void)
948 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
949 getprogname());
950 exit(1);
953 struct gotadmin_list_pack_cb_args {
954 int nblobs;
955 int ntrees;
956 int ncommits;
957 int ntags;
958 int noffdeltas;
959 int nrefdeltas;
960 int human_readable;
961 };
963 static const struct got_error *
964 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
965 off_t size, off_t base_offset, struct got_object_id *base_id)
967 const struct got_error *err;
968 struct gotadmin_list_pack_cb_args *a = arg;
969 char *id_str, *delta_str = NULL, *base_id_str = NULL;
970 const char *type_str;
972 err = got_object_id_str(&id_str, id);
973 if (err)
974 return err;
976 switch (type) {
977 case GOT_OBJ_TYPE_BLOB:
978 type_str = GOT_OBJ_LABEL_BLOB;
979 a->nblobs++;
980 break;
981 case GOT_OBJ_TYPE_TREE:
982 type_str = GOT_OBJ_LABEL_TREE;
983 a->ntrees++;
984 break;
985 case GOT_OBJ_TYPE_COMMIT:
986 type_str = GOT_OBJ_LABEL_COMMIT;
987 a->ncommits++;
988 break;
989 case GOT_OBJ_TYPE_TAG:
990 type_str = GOT_OBJ_LABEL_TAG;
991 a->ntags++;
992 break;
993 case GOT_OBJ_TYPE_OFFSET_DELTA:
994 type_str = "offset-delta";
995 if (asprintf(&delta_str, " base-offset %lld",
996 (long long)base_offset) == -1) {
997 err = got_error_from_errno("asprintf");
998 goto done;
1000 a->noffdeltas++;
1001 break;
1002 case GOT_OBJ_TYPE_REF_DELTA:
1003 type_str = "ref-delta";
1004 err = got_object_id_str(&base_id_str, base_id);
1005 if (err)
1006 goto done;
1007 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1008 err = got_error_from_errno("asprintf");
1009 goto done;
1011 a->nrefdeltas++;
1012 break;
1013 default:
1014 err = got_error(GOT_ERR_OBJ_TYPE);
1015 goto done;
1017 if (a->human_readable) {
1018 char scaled[FMT_SCALED_STRSIZE];
1019 char *s;;
1020 if (fmt_scaled(size, scaled) == -1) {
1021 err = got_error_from_errno("fmt_scaled");
1022 goto done;
1024 s = scaled;
1025 while (isspace((unsigned char)*s))
1026 s++;
1027 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1028 (long long)offset, s, delta_str ? delta_str : "");
1029 } else {
1030 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1031 (long long)offset, (long long)size,
1032 delta_str ? delta_str : "");
1034 done:
1035 free(id_str);
1036 free(base_id_str);
1037 free(delta_str);
1038 return err;
1041 static const struct got_error *
1042 cmd_listpack(int argc, char *argv[])
1044 const struct got_error *error = NULL;
1045 struct got_repository *repo = NULL;
1046 int ch;
1047 struct got_object_id *pack_hash = NULL;
1048 char *packfile_path = NULL;
1049 char *id_str = NULL;
1050 struct gotadmin_list_pack_cb_args lpa;
1051 FILE *packfile = NULL;
1052 int show_stats = 0, human_readable = 0;
1053 int *pack_fds = NULL;
1055 #ifndef PROFILE
1056 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1057 NULL) == -1)
1058 err(1, "pledge");
1059 #endif
1061 while ((ch = getopt(argc, argv, "hs")) != -1) {
1062 switch (ch) {
1063 case 'h':
1064 human_readable = 1;
1065 break;
1066 case 's':
1067 show_stats = 1;
1068 break;
1069 default:
1070 usage_listpack();
1071 /* NOTREACHED */
1075 argc -= optind;
1076 argv += optind;
1078 if (argc != 1)
1079 usage_listpack();
1080 packfile_path = realpath(argv[0], NULL);
1081 if (packfile_path == NULL)
1082 return got_error_from_errno2("realpath", argv[0]);
1084 error = got_repo_pack_fds_open(&pack_fds);
1085 if (error != NULL)
1086 goto done;
1087 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1088 if (error)
1089 goto done;
1090 #ifndef PROFILE
1091 /* Remove "cpath" promise. */
1092 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1093 NULL) == -1)
1094 err(1, "pledge");
1095 #endif
1096 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1097 if (error)
1098 goto done;
1100 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1101 packfile_path);
1102 if (error)
1103 goto done;
1104 error = got_object_id_str(&id_str, pack_hash);
1105 if (error)
1106 goto done;
1108 memset(&lpa, 0, sizeof(lpa));
1109 lpa.human_readable = human_readable;
1110 error = got_repo_list_pack(packfile, pack_hash, repo,
1111 list_pack_cb, &lpa, check_cancelled, NULL);
1112 if (error)
1113 goto done;
1114 if (show_stats) {
1115 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1116 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1117 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1118 lpa.noffdeltas + lpa.nrefdeltas,
1119 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1120 lpa.noffdeltas, lpa.nrefdeltas);
1122 done:
1123 if (repo)
1124 got_repo_close(repo);
1125 if (pack_fds) {
1126 const struct got_error *pack_err =
1127 got_repo_pack_fds_close(pack_fds);
1128 if (error == NULL)
1129 error = pack_err;
1131 free(id_str);
1132 free(pack_hash);
1133 free(packfile_path);
1134 return error;
1137 __dead static void
1138 usage_cleanup(void)
1140 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1141 getprogname());
1142 exit(1);
1145 struct got_cleanup_progress_arg {
1146 int last_nloose;
1147 int last_ncommits;
1148 int last_npurged;
1149 int last_nredundant;
1150 int verbosity;
1151 int printed_something;
1152 int dry_run;
1155 static const struct got_error *
1156 cleanup_progress(void *arg, int ncommits, int nloose, int npurged,
1157 int nredundant)
1159 struct got_cleanup_progress_arg *a = arg;
1160 int print_loose = 0, print_commits = 0, print_purged = 0;
1161 int print_redundant = 0;
1163 if (a->last_ncommits != ncommits) {
1164 print_commits = 1;
1165 a->last_ncommits = ncommits;
1167 if (a->last_nloose != nloose) {
1168 print_commits = 1;
1169 print_loose = 1;
1170 a->last_nloose = nloose;
1172 if (a->last_npurged != npurged) {
1173 print_commits = 1;
1174 print_loose = 1;
1175 print_purged = 1;
1176 a->last_npurged = npurged;
1178 if (a->last_nredundant != nredundant) {
1179 print_commits = 1;
1180 print_loose = 1;
1181 print_purged = 1;
1182 print_redundant = 1;
1183 a->last_nredundant = nredundant;
1186 if (a->verbosity < 0)
1187 return NULL;
1189 if (print_loose || print_commits || print_purged || print_redundant)
1190 printf("\r");
1191 if (print_commits)
1192 printf("%d commit%s scanned", ncommits,
1193 ncommits == 1 ? "" : "s");
1194 if (print_loose)
1195 printf("; %d loose object%s", nloose, nloose == 1 ? "" : "s");
1196 if (print_purged || print_redundant) {
1197 if (a->dry_run) {
1198 printf("; could purge %d object%s", npurged,
1199 npurged == 1 ? "" : "s");
1200 } else {
1201 printf("; purged %d object%s", npurged,
1202 npurged == 1 ? "" : "s");
1205 if (print_redundant) {
1206 if (a->dry_run) {
1207 printf(", %d pack file%s", nredundant,
1208 nredundant == 1 ? "" : "s");
1209 } else {
1210 printf(", %d pack file%s", nredundant,
1211 nredundant == 1 ? "" : "s");
1214 if (print_loose || print_commits || print_purged || print_redundant) {
1215 a->printed_something = 1;
1216 fflush(stdout);
1218 return NULL;
1221 struct got_lonely_packidx_progress_arg {
1222 int verbosity;
1223 int printed_something;
1224 int dry_run;
1227 static const struct got_error *
1228 lonely_packidx_progress(void *arg, const char *path)
1230 struct got_lonely_packidx_progress_arg *a = arg;
1232 if (a->verbosity < 0)
1233 return NULL;
1235 if (a->dry_run)
1236 printf("%s could be removed\n", path);
1237 else
1238 printf("%s removed\n", path);
1240 a->printed_something = 1;
1241 return NULL;
1244 static const struct got_error *
1245 cmd_cleanup(int argc, char *argv[])
1247 const struct got_error *error = NULL;
1248 char *repo_path = NULL;
1249 struct got_repository *repo = NULL;
1250 int ch, dry_run = 0, verbosity = 0;
1251 int ncommits = 0, nloose = 0, npacked = 0;
1252 int remove_lonely_packidx = 0, ignore_mtime = 0;
1253 struct got_cleanup_progress_arg cpa;
1254 struct got_lonely_packidx_progress_arg lpa;
1255 off_t loose_before, loose_after;
1256 off_t pack_before, pack_after;
1257 off_t total_size;
1258 char loose_before_scaled[FMT_SCALED_STRSIZE];
1259 char loose_after_scaled[FMT_SCALED_STRSIZE];
1260 char pack_before_scaled[FMT_SCALED_STRSIZE];
1261 char pack_after_scaled[FMT_SCALED_STRSIZE];
1262 char total_size_scaled[FMT_SCALED_STRSIZE];
1263 int *pack_fds = NULL;
1265 #ifndef PROFILE
1266 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1267 NULL) == -1)
1268 err(1, "pledge");
1269 #endif
1271 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1272 switch (ch) {
1273 case 'a':
1274 ignore_mtime = 1;
1275 break;
1276 case 'n':
1277 dry_run = 1;
1278 break;
1279 case 'p':
1280 remove_lonely_packidx = 1;
1281 break;
1282 case 'q':
1283 verbosity = -1;
1284 break;
1285 case 'r':
1286 repo_path = realpath(optarg, NULL);
1287 if (repo_path == NULL)
1288 return got_error_from_errno2("realpath",
1289 optarg);
1290 got_path_strip_trailing_slashes(repo_path);
1291 break;
1292 default:
1293 usage_cleanup();
1294 /* NOTREACHED */
1298 argc -= optind;
1299 argv += optind;
1301 if (repo_path == NULL) {
1302 error = get_repo_path(&repo_path);
1303 if (error)
1304 goto done;
1306 error = got_repo_pack_fds_open(&pack_fds);
1307 if (error != NULL)
1308 goto done;
1309 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1310 if (error)
1311 goto done;
1313 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1314 if (error)
1315 goto done;
1317 if (got_repo_has_extension(repo, "preciousObjects")) {
1318 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1319 "the preciousObjects Git extension is enabled; "
1320 "this implies that objects must not be deleted");
1321 goto done;
1324 if (remove_lonely_packidx) {
1325 memset(&lpa, 0, sizeof(lpa));
1326 lpa.dry_run = dry_run;
1327 lpa.verbosity = verbosity;
1328 error = got_repo_remove_lonely_packidx(repo, dry_run,
1329 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1330 goto done;
1333 memset(&cpa, 0, sizeof(cpa));
1334 cpa.last_nloose = -1;
1335 cpa.last_npurged = -1;
1336 cpa.last_nredundant = -1;
1337 cpa.dry_run = dry_run;
1338 cpa.verbosity = verbosity;
1340 error = got_repo_cleanup(repo, &loose_before, &loose_after,
1341 &pack_before, &pack_after, &ncommits, &nloose, &npacked,
1342 dry_run, ignore_mtime, cleanup_progress, &cpa,
1343 check_cancelled, NULL);
1344 if (cpa.printed_something)
1345 printf("\n");
1346 if (error)
1347 goto done;
1349 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1351 if (cpa.printed_something) {
1352 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1353 error = got_error_from_errno("fmt_scaled");
1354 goto done;
1356 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1357 error = got_error_from_errno("fmt_scaled");
1358 goto done;
1360 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1361 error = got_error_from_errno("fmt_scaled");
1362 goto done;
1364 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1365 error = got_error_from_errno("fmt_scaled");
1366 goto done;
1368 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1369 error = got_error_from_errno("fmt_scaled");
1370 goto done;
1372 printf("loose total size before: %s\n", loose_before_scaled);
1373 printf("loose total size after: %s\n", loose_after_scaled);
1374 printf("pack files total size before: %s\n",
1375 pack_before_scaled);
1376 printf("pack files total size after: %s\n", pack_after_scaled);
1377 if (dry_run) {
1378 printf("disk space which would be freed: %s\n",
1379 total_size_scaled);
1380 } else
1381 printf("disk space freed: %s\n", total_size_scaled);
1382 printf("loose objects also found in pack files: %d\n", npacked);
1385 done:
1386 if (repo)
1387 got_repo_close(repo);
1388 if (pack_fds) {
1389 const struct got_error *pack_err =
1390 got_repo_pack_fds_close(pack_fds);
1391 if (error == NULL)
1392 error = pack_err;
1394 free(repo_path);
1395 return error;
1398 __dead static void
1399 usage_dump(void)
1401 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1402 "[-x reference] [reference]...\n", getprogname());
1403 exit(1);
1406 static const struct got_error *
1407 cmd_dump(int argc, char *argv[])
1409 const struct got_error *error = NULL;
1410 struct got_pack_progress_arg ppa;
1411 struct got_repository *repo = NULL;
1412 struct got_pathlist_head exclude_args;
1413 struct got_pathlist_entry *pe;
1414 struct got_reflist_head exclude_refs;
1415 struct got_reflist_head include_refs;
1416 struct got_reflist_entry *re, *new;
1417 const char *refname;
1418 char *repo_path = NULL;
1419 int *pack_fds = NULL;
1420 int verbosity = 0;
1421 int i, ch;
1423 TAILQ_INIT(&exclude_args);
1424 TAILQ_INIT(&exclude_refs);
1425 TAILQ_INIT(&include_refs);
1427 #ifndef PROFILE
1428 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1429 NULL) == -1)
1430 err(1, "pledge");
1431 #endif
1433 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1434 switch (ch) {
1435 case 'q':
1436 verbosity = -1;
1437 break;
1438 case 'r':
1439 repo_path = realpath(optarg, NULL);
1440 if (repo_path == NULL)
1441 return got_error_from_errno2("realpath",
1442 optarg);
1443 got_path_strip_trailing_slashes(repo_path);
1444 break;
1445 case 'x':
1446 error = got_pathlist_append(&exclude_args,
1447 optarg, NULL);
1448 if (error)
1449 return error;
1450 break;
1451 default:
1452 usage_dump();
1453 /* NOTREACHED */
1456 argc -= optind;
1457 argv += optind;
1459 if (repo_path == NULL) {
1460 error = get_repo_path(&repo_path);
1461 if (error)
1462 goto done;
1464 error = got_repo_pack_fds_open(&pack_fds);
1465 if (error != NULL)
1466 goto done;
1467 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1468 if (error)
1469 goto done;
1471 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1472 if (error)
1473 goto done;
1475 TAILQ_FOREACH(pe, &exclude_args, entry) {
1476 refname = pe->path;
1477 error = add_ref(&new, &exclude_refs, refname, repo);
1478 if (error)
1479 goto done;
1482 if (argc == 0) {
1483 error = got_ref_list(&include_refs, repo, "",
1484 got_ref_cmp_by_name, NULL);
1485 if (error)
1486 goto done;
1487 } else {
1488 for (i = 0; i < argc; i++) {
1489 got_path_strip_trailing_slashes(argv[i]);
1490 refname = argv[i];
1491 error = add_ref(&new, &include_refs, refname, repo);
1492 if (error)
1493 goto done;
1497 /* Ignore references in the refs/got/ namespace. */
1498 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1499 refname = got_ref_get_name(re->ref);
1500 if (strncmp("refs/got/", refname, 9) != 0)
1501 continue;
1502 TAILQ_REMOVE(&include_refs, re, entry);
1503 got_ref_close(re->ref);
1504 free(re);
1507 memset(&ppa, 0, sizeof(ppa));
1508 ppa.out = stderr;
1509 ppa.verbosity = verbosity;
1511 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1512 repo, pack_progress, &ppa, check_cancelled, NULL);
1513 if (ppa.printed_something)
1514 fprintf(stderr, "\n");
1515 done:
1516 if (repo)
1517 got_repo_close(repo);
1519 if (pack_fds) {
1520 const struct got_error *pack_err;
1522 pack_err = got_repo_pack_fds_close(pack_fds);
1523 if (error == NULL)
1524 error = pack_err;
1527 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1528 got_ref_list_free(&exclude_refs);
1529 got_ref_list_free(&include_refs);
1530 free(repo_path);
1532 return error;
1535 __dead static void
1536 usage_load(void)
1538 fprintf(stderr, "usage: %s load [-nq] [-l bundle-file] "
1539 "[-r repository-path] [reference ...]\n",
1540 getprogname());
1541 exit(1);
1544 static const struct got_error *
1545 load_progress(void *arg, off_t packfile_size, int nobj_total,
1546 int nobj_indexed, int nobj_loose, int nobj_resolved)
1548 return pack_index_progress(arg, packfile_size, nobj_total,
1549 nobj_indexed, nobj_loose, nobj_resolved);
1552 static int
1553 is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1555 struct got_pathlist_entry *pe;
1557 if (TAILQ_EMPTY(wanted))
1558 return 1;
1560 TAILQ_FOREACH(pe, wanted, entry) {
1561 if (strcmp(pe->path, ref) == 0)
1562 return 1;
1565 return 0;
1568 static const struct got_error *
1569 create_ref(const char *refname, struct got_object_id *id,
1570 int verbosity, struct got_repository *repo)
1572 const struct got_error *err = NULL;
1573 struct got_reference *ref;
1574 char *id_str;
1576 err = got_object_id_str(&id_str, id);
1577 if (err)
1578 return err;
1580 err = got_ref_alloc(&ref, refname, id);
1581 if (err)
1582 goto done;
1584 err = got_ref_write(ref, repo);
1585 got_ref_close(ref);
1587 if (err == NULL && verbosity >= 0)
1588 printf("Created reference %s: %s\n", refname, id_str);
1589 done:
1590 free(id_str);
1591 return err;
1594 static const struct got_error *
1595 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1596 int replace_tags, int verbosity, struct got_repository *repo)
1598 const struct got_error *err = NULL;
1599 char *new_id_str = NULL;
1600 struct got_object_id *old_id = NULL;
1602 err = got_object_id_str(&new_id_str, new_id);
1603 if (err)
1604 goto done;
1606 if (!replace_tags &&
1607 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1608 err = got_ref_resolve(&old_id, repo, ref);
1609 if (err)
1610 goto done;
1611 if (got_object_id_cmp(old_id, new_id) == 0)
1612 goto done;
1613 if (verbosity >= 0) {
1614 printf("Rejecting update of existing tag %s: %s\n",
1615 got_ref_get_name(ref), new_id_str);
1617 goto done;
1620 if (got_ref_is_symbolic(ref)) {
1621 if (verbosity >= 0) {
1622 printf("Replacing reference %s: %s\n",
1623 got_ref_get_name(ref),
1624 got_ref_get_symref_target(ref));
1626 err = got_ref_change_symref_to_ref(ref, new_id);
1627 if (err)
1628 goto done;
1629 err = got_ref_write(ref, repo);
1630 if (err)
1631 goto done;
1632 } else {
1633 err = got_ref_resolve(&old_id, repo, ref);
1634 if (err)
1635 goto done;
1636 if (got_object_id_cmp(old_id, new_id) == 0)
1637 goto done;
1639 err = got_ref_change_ref(ref, new_id);
1640 if (err)
1641 goto done;
1642 err = got_ref_write(ref, repo);
1643 if (err)
1644 goto done;
1647 if (verbosity >= 0)
1648 printf("Updated %s: %s\n", got_ref_get_name(ref),
1649 new_id_str);
1650 done:
1651 free(old_id);
1652 free(new_id_str);
1653 return err;
1656 static const struct got_error *
1657 cmd_load(int argc, char *argv[])
1659 const struct got_error *error = NULL;
1660 struct got_repository *repo = NULL;
1661 struct got_pathlist_head include_args;
1662 struct got_pathlist_head available_refs;
1663 struct got_pathlist_entry *pe;
1664 struct got_pack_progress_arg ppa;
1665 FILE *in = stdin;
1666 int *pack_fds = NULL;
1667 char *repo_path = NULL;
1668 int list_refs_only = 0;
1669 int noop = 0;
1670 int verbosity = 0;
1671 int ch, i;
1673 TAILQ_INIT(&include_args);
1674 TAILQ_INIT(&available_refs);
1676 #ifndef PROFILE
1677 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1678 "sendfd unveil", NULL) == -1)
1679 err(1, "pledge");
1680 #endif
1682 while ((ch = getopt(argc, argv, "l:nqr:")) != -1) {
1683 switch (ch) {
1684 case 'l':
1685 list_refs_only = 1;
1686 in = fopen(optarg, "re");
1687 if (in == NULL)
1688 return got_error_from_errno2("open", optarg);
1689 break;
1690 case 'n':
1691 noop = 1;
1692 break;
1693 case 'q':
1694 verbosity = -1;
1695 break;
1696 case 'r':
1697 repo_path = realpath(optarg, NULL);
1698 if (repo_path == NULL)
1699 return got_error_from_errno2("realpath",
1700 optarg);
1701 got_path_strip_trailing_slashes(repo_path);
1702 break;
1703 default:
1704 usage_load();
1705 /* NOTREACHED */
1708 argc -= optind;
1709 argv += optind;
1711 if (list_refs_only && argc > 1)
1712 errx(1, "-l and references on the command line are exclusive");
1713 if (list_refs_only && noop)
1714 errx(1, "-n and -l are mutually exclusive");
1716 for (i = 0; i < argc; i++) {
1717 char *refname = argv[i];
1718 got_path_strip_trailing_slashes(refname);
1719 if (!got_ref_name_is_valid(refname))
1720 errx(1, "invalid reference name %s", refname);
1721 error = got_pathlist_append(&include_args, refname, NULL);
1722 if (error)
1723 goto done;
1726 if (repo_path == NULL) {
1727 error = get_repo_path(&repo_path);
1728 if (error)
1729 goto done;
1731 error = got_repo_pack_fds_open(&pack_fds);
1732 if (error != NULL)
1733 goto done;
1734 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1735 if (error)
1736 goto done;
1738 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1739 if (error)
1740 goto done;
1742 memset(&ppa, 0, sizeof(ppa));
1743 ppa.out = stdout;
1744 ppa.verbosity = verbosity;
1746 error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1747 load_progress, &ppa, check_cancelled, NULL);
1748 if (verbosity >= 0 && !list_refs_only)
1749 printf("\n");
1750 if (error)
1751 goto done;
1753 if (list_refs_only) {
1754 TAILQ_FOREACH(pe, &available_refs, entry) {
1755 const char *refname = pe->path;
1756 struct got_object_id *id = pe->data;
1757 char *idstr;
1759 error = got_object_id_str(&idstr, id);
1760 if (error)
1761 goto done;
1763 printf("%s: %s\n", refname, idstr);
1764 free(idstr);
1766 goto done;
1769 if (noop)
1770 goto done;
1772 /* Update references */
1773 TAILQ_FOREACH(pe, &available_refs, entry) {
1774 const struct got_error *unlock_err;
1775 struct got_reference *ref;
1776 const char *refname = pe->path;
1777 struct got_object_id *id = pe->data;
1779 if (!is_wanted_ref(&include_args, pe->path))
1780 continue;
1782 error = got_ref_open(&ref, repo, refname, 1);
1783 if (error) {
1784 if (error->code != GOT_ERR_NOT_REF)
1785 goto done;
1786 error = create_ref(refname, id, verbosity, repo);
1787 if (error)
1788 goto done;
1789 } else {
1790 /* XXX: check advances only and add -f to force? */
1791 error = update_ref(ref, id, 1, verbosity, repo);
1792 unlock_err = got_ref_unlock(ref);
1793 if (unlock_err && error == NULL)
1794 error = unlock_err;
1795 got_ref_close(ref);
1796 if (error)
1797 goto done;
1801 done:
1802 if (in != stdin && fclose(in) == EOF && error == NULL)
1803 error = got_error_from_errno("fclose");
1805 if (repo)
1806 got_repo_close(repo);
1808 if (pack_fds) {
1809 const struct got_error *pack_err;
1811 pack_err = got_repo_pack_fds_close(pack_fds);
1812 if (error == NULL)
1813 error = pack_err;
1816 got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1817 got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);
1818 free(repo_path);
1820 return error;