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 <sys/queue.h>
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <getopt.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <inttypes.h>
26 #include <sha1.h>
27 #include <sha2.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <util.h>
35 #include "got_version.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_cancel.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_repository_dump.h"
43 #include "got_repository_load.h"
44 #include "got_gotconfig.h"
45 #include "got_path.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
48 #include "got_worktree.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static void
64 catch_sigpipe(int signo)
65 {
66 sigpipe_received = 1;
67 }
69 static const struct got_error *
70 check_cancelled(void *arg)
71 {
72 if (sigint_received || sigpipe_received)
73 return got_error(GOT_ERR_CANCELLED);
74 return NULL;
75 }
77 struct gotadmin_cmd {
78 const char *cmd_name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
82 };
84 __dead static void usage(int, int);
85 __dead static void usage_init(void);
86 __dead static void usage_info(void);
87 __dead static void usage_pack(void);
88 __dead static void usage_indexpack(void);
89 __dead static void usage_listpack(void);
90 __dead static void usage_cleanup(void);
91 __dead static void usage_dump(void);
92 __dead static void usage_load(void);
94 static const struct got_error* cmd_init(int, char *[]);
95 static const struct got_error* cmd_info(int, char *[]);
96 static const struct got_error* cmd_pack(int, char *[]);
97 static const struct got_error* cmd_indexpack(int, char *[]);
98 static const struct got_error* cmd_listpack(int, char *[]);
99 static const struct got_error* cmd_cleanup(int, char *[]);
100 static const struct got_error* cmd_dump(int, char *[]);
101 static const struct got_error* cmd_load(int, char *[]);
103 static const struct gotadmin_cmd gotadmin_commands[] = {
104 { "init", cmd_init, usage_init, "" },
105 { "info", cmd_info, usage_info, "" },
106 { "pack", cmd_pack, usage_pack, "" },
107 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
108 { "listpack", cmd_listpack, usage_listpack, "ls" },
109 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
110 { "dump", cmd_dump, usage_dump, "" },
111 { "load", cmd_load, usage_load, "" },
112 };
114 static void
115 list_commands(FILE *fp)
117 size_t i;
119 fprintf(fp, "commands:");
120 for (i = 0; i < nitems(gotadmin_commands); i++) {
121 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
122 fprintf(fp, " %s", cmd->cmd_name);
124 fputc('\n', fp);
127 int
128 main(int argc, char *argv[])
130 const struct gotadmin_cmd *cmd;
131 size_t i;
132 int ch;
133 int hflag = 0, Vflag = 0;
134 static const struct option longopts[] = {
135 { "version", no_argument, NULL, 'V' },
136 { NULL, 0, NULL, 0 }
137 };
139 setlocale(LC_CTYPE, "");
141 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
142 switch (ch) {
143 case 'h':
144 hflag = 1;
145 break;
146 case 'V':
147 Vflag = 1;
148 break;
149 default:
150 usage(hflag, 1);
151 /* NOTREACHED */
155 argc -= optind;
156 argv += optind;
157 optind = 1;
158 optreset = 1;
160 if (Vflag) {
161 got_version_print_str();
162 return 0;
165 if (argc <= 0)
166 usage(hflag, hflag ? 0 : 1);
168 signal(SIGINT, catch_sigint);
169 signal(SIGPIPE, catch_sigpipe);
171 for (i = 0; i < nitems(gotadmin_commands); i++) {
172 const struct got_error *error;
174 cmd = &gotadmin_commands[i];
176 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
177 strcmp(cmd->cmd_alias, argv[0]) != 0)
178 continue;
180 if (hflag)
181 cmd->cmd_usage();
183 error = cmd->cmd_main(argc, argv);
184 if (error && error->code != GOT_ERR_CANCELLED &&
185 error->code != GOT_ERR_PRIVSEP_EXIT &&
186 !(sigpipe_received &&
187 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
188 !(sigint_received &&
189 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
190 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
191 return 1;
194 return 0;
197 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
198 list_commands(stderr);
199 return 1;
202 __dead static void
203 usage(int hflag, int status)
205 FILE *fp = (status == 0) ? stdout : stderr;
207 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
208 getprogname());
209 if (hflag)
210 list_commands(fp);
211 exit(status);
214 static const struct got_error *
215 apply_unveil(const char *repo_path, int repo_read_only)
217 const struct got_error *err;
219 #ifdef PROFILE
220 if (unveil("gmon.out", "rwc") != 0)
221 return got_error_from_errno2("unveil", "gmon.out");
222 #endif
223 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
224 return got_error_from_errno2("unveil", repo_path);
226 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
227 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
229 err = got_privsep_unveil_exec_helpers();
230 if (err != NULL)
231 return err;
233 if (unveil(NULL, NULL) != 0)
234 return got_error_from_errno("unveil");
236 return NULL;
239 __dead static void
240 usage_info(void)
242 fprintf(stderr, "usage: %s info [-r repository-path]\n",
243 getprogname());
244 exit(1);
247 static const struct got_error *
248 get_repo_path(char **repo_path)
250 const struct got_error *err = NULL;
251 struct got_worktree *worktree = NULL;
252 char *cwd;
254 *repo_path = NULL;
256 cwd = getcwd(NULL, 0);
257 if (cwd == NULL)
258 return got_error_from_errno("getcwd");
260 err = got_worktree_open(&worktree, cwd, NULL);
261 if (err) {
262 if (err->code != GOT_ERR_NOT_WORKTREE)
263 goto done;
264 err = NULL;
267 if (worktree)
268 *repo_path = strdup(got_worktree_get_repo_path(worktree));
269 else
270 *repo_path = strdup(cwd);
271 if (*repo_path == NULL)
272 err = got_error_from_errno("strdup");
273 done:
274 if (worktree)
275 got_worktree_close(worktree);
276 free(cwd);
277 return err;
280 __dead static void
281 usage_init(void)
283 fprintf(stderr, "usage: %s init [-b branch] 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 int ch;
296 #ifndef PROFILE
297 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
298 err(1, "pledge");
299 #endif
301 while ((ch = getopt(argc, argv, "b:")) != -1) {
302 switch (ch) {
303 case 'b':
304 head_name = optarg;
305 break;
306 default:
307 usage_init();
308 /* NOTREACHED */
312 argc -= optind;
313 argv += optind;
315 if (argc != 1)
316 usage_init();
318 repo_path = strdup(argv[0]);
319 if (repo_path == NULL)
320 return got_error_from_errno("strdup");
322 got_path_strip_trailing_slashes(repo_path);
324 error = got_path_mkdir(repo_path);
325 if (error &&
326 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
327 goto done;
329 error = apply_unveil(repo_path, 0);
330 if (error)
331 goto done;
333 error = got_repo_init(repo_path, head_name);
334 done:
335 free(repo_path);
336 return error;
339 static const struct got_error *
340 cmd_info(int argc, char *argv[])
342 const struct got_error *error = NULL;
343 char *repo_path = NULL;
344 struct got_repository *repo = NULL;
345 const struct got_gotconfig *gotconfig = NULL;
346 int ch, npackfiles, npackedobj, nobj;
347 off_t packsize, loose_size;
348 char scaled[FMT_SCALED_STRSIZE];
349 int *pack_fds = NULL;
351 #ifndef PROFILE
352 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
353 NULL) == -1)
354 err(1, "pledge");
355 #endif
357 while ((ch = getopt(argc, argv, "r:")) != -1) {
358 switch (ch) {
359 case 'r':
360 repo_path = realpath(optarg, NULL);
361 if (repo_path == NULL)
362 return got_error_from_errno2("realpath",
363 optarg);
364 got_path_strip_trailing_slashes(repo_path);
365 break;
366 default:
367 usage_info();
368 /* NOTREACHED */
372 argc -= optind;
373 argv += optind;
375 if (repo_path == NULL) {
376 error = get_repo_path(&repo_path);
377 if (error)
378 goto done;
380 error = got_repo_pack_fds_open(&pack_fds);
381 if (error != NULL)
382 goto done;
383 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
384 if (error)
385 goto done;
386 #ifndef PROFILE
387 /* Remove "cpath" promise. */
388 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
389 NULL) == -1)
390 err(1, "pledge");
391 #endif
392 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
393 if (error)
394 goto done;
396 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
398 gotconfig = got_repo_get_gotconfig(repo);
399 if (gotconfig) {
400 const struct got_remote_repo *remotes;
401 int i, nremotes;
402 if (got_gotconfig_get_author(gotconfig)) {
403 printf("default author: %s\n",
404 got_gotconfig_get_author(gotconfig));
406 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
407 for (i = 0; i < nremotes; i++) {
408 const char *fetch_url = remotes[i].fetch_url;
409 const char *send_url = remotes[i].send_url;
410 if (strcmp(fetch_url, send_url) == 0) {
411 printf("remote \"%s\": %s\n", remotes[i].name,
412 remotes[i].fetch_url);
413 } else {
414 printf("remote \"%s\" (fetch): %s\n",
415 remotes[i].name, remotes[i].fetch_url);
416 printf("remote \"%s\" (send): %s\n",
417 remotes[i].name, remotes[i].send_url);
422 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
423 &packsize, repo);
424 if (error)
425 goto done;
426 printf("pack files: %d\n", npackfiles);
427 if (npackfiles > 0) {
428 if (fmt_scaled(packsize, scaled) == -1) {
429 error = got_error_from_errno("fmt_scaled");
430 goto done;
432 printf("packed objects: %d\n", npackedobj);
433 printf("packed total size: %s\n", scaled);
436 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
437 if (error)
438 goto done;
439 printf("loose objects: %d\n", nobj);
440 if (nobj > 0) {
441 if (fmt_scaled(loose_size, scaled) == -1) {
442 error = got_error_from_errno("fmt_scaled");
443 goto done;
445 printf("loose total size: %s\n", scaled);
447 done:
448 if (repo)
449 got_repo_close(repo);
450 if (pack_fds) {
451 const struct got_error *pack_err =
452 got_repo_pack_fds_close(pack_fds);
453 if (error == NULL)
454 error = pack_err;
457 free(repo_path);
458 return error;
461 __dead static void
462 usage_pack(void)
464 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
465 "[-x reference] [reference ...]\n", getprogname());
466 exit(1);
469 struct got_pack_progress_arg {
470 FILE *out;
471 char last_scaled_size[FMT_SCALED_STRSIZE];
472 int last_ncolored;
473 int last_nfound;
474 int last_ntrees;
475 int loading_done;
476 int last_ncommits;
477 int last_nobj_total;
478 int last_p_deltify;
479 int last_p_written;
480 int last_p_indexed;
481 int last_p_resolved;
482 int verbosity;
483 int printed_something;
484 };
486 static void
487 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
488 int ncolored, int nfound, int ntrees)
490 if (print_colored) {
491 fprintf(out, "%d commit%s colored", ncolored,
492 ncolored == 1 ? "" : "s");
494 if (print_found) {
495 fprintf(out, "%s%d object%s found",
496 ncolored > 0 ? "; " : "",
497 nfound, nfound == 1 ? "" : "s");
499 if (print_trees) {
500 fprintf(out, "; %d tree%s scanned", ntrees,
501 ntrees == 1 ? "" : "s");
505 static const struct got_error *
506 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
507 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
508 int nobj_written)
510 struct got_pack_progress_arg *a = arg;
511 char scaled_size[FMT_SCALED_STRSIZE];
512 int p_deltify, p_written;
513 int print_colored = 0, print_found = 0, print_trees = 0;
514 int print_searching = 0, print_total = 0;
515 int print_deltify = 0, print_written = 0;
517 if (a->verbosity < 0)
518 return NULL;
520 if (a->last_ncolored != ncolored) {
521 print_colored = 1;
522 a->last_ncolored = ncolored;
525 if (a->last_nfound != nfound) {
526 print_colored = 1;
527 print_found = 1;
528 a->last_nfound = nfound;
531 if (a->last_ntrees != ntrees) {
532 print_colored = 1;
533 print_found = 1;
534 print_trees = 1;
535 a->last_ntrees = ntrees;
538 if ((print_colored || print_found || print_trees) &&
539 !a->loading_done) {
540 fprintf(a->out, "\r");
541 print_load_info(a->out, print_colored, print_found,
542 print_trees, ncolored, nfound, ntrees);
543 a->printed_something = 1;
544 fflush(a->out);
545 return NULL;
546 } else if (!a->loading_done) {
547 fprintf(a->out, "\r");
548 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
549 fprintf(a->out, "\n");
550 a->loading_done = 1;
553 if (fmt_scaled(packfile_size, scaled_size) == -1)
554 return got_error_from_errno("fmt_scaled");
556 if (a->last_ncommits != ncommits) {
557 print_searching = 1;
558 a->last_ncommits = ncommits;
561 if (a->last_nobj_total != nobj_total) {
562 print_searching = 1;
563 print_total = 1;
564 a->last_nobj_total = nobj_total;
567 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
568 strcmp(scaled_size, a->last_scaled_size)) != 0) {
569 if (strlcpy(a->last_scaled_size, scaled_size,
570 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
571 return got_error(GOT_ERR_NO_SPACE);
574 if (nobj_deltify > 0 || nobj_written > 0) {
575 if (nobj_deltify > 0) {
576 p_deltify = (nobj_deltify * 100) / nobj_total;
577 if (p_deltify != a->last_p_deltify) {
578 a->last_p_deltify = p_deltify;
579 print_searching = 1;
580 print_total = 1;
581 print_deltify = 1;
584 if (nobj_written > 0) {
585 p_written = (nobj_written * 100) / nobj_total;
586 if (p_written != a->last_p_written) {
587 a->last_p_written = p_written;
588 print_searching = 1;
589 print_total = 1;
590 print_deltify = 1;
591 print_written = 1;
596 if (print_searching || print_total || print_deltify || print_written)
597 fprintf(a->out, "\r");
598 if (print_searching)
599 fprintf(a->out, "packing %d reference%s", ncommits,
600 ncommits == 1 ? "" : "s");
601 if (print_total)
602 fprintf(a->out, "; %d object%s", nobj_total,
603 nobj_total == 1 ? "" : "s");
604 if (print_deltify)
605 fprintf(a->out, "; deltify: %d%%", p_deltify);
606 if (print_written)
607 fprintf(a->out, "; writing pack: %*s %d%%",
608 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
609 if (print_searching || print_total || print_deltify ||
610 print_written) {
611 a->printed_something = 1;
612 fflush(a->out);
614 return NULL;
617 static const struct got_error *
618 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
619 int nobj_indexed, int nobj_loose, int nobj_resolved)
621 struct got_pack_progress_arg *a = arg;
622 char scaled_size[FMT_SCALED_STRSIZE];
623 int p_indexed, p_resolved;
624 int print_size = 0, print_indexed = 0, print_resolved = 0;
626 if (a->verbosity < 0)
627 return NULL;
629 if (packfile_size > 0 || nobj_indexed > 0) {
630 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
631 (a->last_scaled_size[0] == '\0' ||
632 strcmp(scaled_size, a->last_scaled_size)) != 0) {
633 print_size = 1;
634 if (strlcpy(a->last_scaled_size, scaled_size,
635 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
636 return got_error(GOT_ERR_NO_SPACE);
638 if (nobj_indexed > 0) {
639 p_indexed = (nobj_indexed * 100) / nobj_total;
640 if (p_indexed != a->last_p_indexed) {
641 a->last_p_indexed = p_indexed;
642 print_indexed = 1;
643 print_size = 1;
646 if (nobj_resolved > 0) {
647 p_resolved = (nobj_resolved * 100) /
648 (nobj_total - nobj_loose);
649 if (p_resolved != a->last_p_resolved) {
650 a->last_p_resolved = p_resolved;
651 print_resolved = 1;
652 print_indexed = 1;
653 print_size = 1;
658 if (print_size || print_indexed || print_resolved)
659 printf("\r");
660 if (print_size)
661 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
662 if (print_indexed)
663 printf("; indexing %d%%", p_indexed);
664 if (print_resolved)
665 printf("; resolving deltas %d%%", p_resolved);
666 if (print_size || print_indexed || print_resolved)
667 fflush(stdout);
669 return NULL;
672 static const struct got_error *
673 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
674 const char *refname, struct got_repository *repo)
676 const struct got_error *err;
677 struct got_reference *ref;
679 *new = NULL;
681 err = got_ref_open(&ref, repo, refname, 0);
682 if (err) {
683 if (err->code != GOT_ERR_NOT_REF)
684 return err;
686 /* Treat argument as a reference prefix. */
687 err = got_ref_list(refs, repo, refname,
688 got_ref_cmp_by_name, NULL);
689 } else {
690 err = got_reflist_insert(new, refs, ref,
691 got_ref_cmp_by_name, NULL);
692 if (err || *new == NULL /* duplicate */)
693 got_ref_close(ref);
696 return err;
699 static const struct got_error *
700 cmd_pack(int argc, char *argv[])
702 const struct got_error *error = NULL;
703 char *repo_path = NULL;
704 struct got_repository *repo = NULL;
705 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
706 struct got_object_id *pack_hash = NULL;
707 char *id_str = NULL;
708 struct got_pack_progress_arg ppa;
709 FILE *packfile = NULL;
710 struct got_pathlist_head exclude_args;
711 struct got_pathlist_entry *pe;
712 struct got_reflist_head exclude_refs;
713 struct got_reflist_head include_refs;
714 struct got_reflist_entry *re, *new;
715 int *pack_fds = NULL;
717 TAILQ_INIT(&exclude_args);
718 TAILQ_INIT(&exclude_refs);
719 TAILQ_INIT(&include_refs);
721 #ifndef PROFILE
722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
723 NULL) == -1)
724 err(1, "pledge");
725 #endif
727 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
728 switch (ch) {
729 case 'a':
730 loose_obj_only = 0;
731 break;
732 case 'D':
733 force_refdelta = 1;
734 break;
735 case 'q':
736 verbosity = -1;
737 break;
738 case 'r':
739 repo_path = realpath(optarg, NULL);
740 if (repo_path == NULL)
741 return got_error_from_errno2("realpath",
742 optarg);
743 got_path_strip_trailing_slashes(repo_path);
744 break;
745 case 'x':
746 got_path_strip_trailing_slashes(optarg);
747 error = got_pathlist_append(&exclude_args,
748 optarg, NULL);
749 if (error)
750 return error;
751 break;
752 default:
753 usage_pack();
754 /* NOTREACHED */
758 argc -= optind;
759 argv += optind;
761 if (repo_path == NULL) {
762 error = get_repo_path(&repo_path);
763 if (error)
764 goto done;
766 error = got_repo_pack_fds_open(&pack_fds);
767 if (error != NULL)
768 goto done;
769 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
770 if (error)
771 goto done;
773 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
774 if (error)
775 goto done;
777 TAILQ_FOREACH(pe, &exclude_args, entry) {
778 const char *refname = pe->path;
779 error = add_ref(&new, &exclude_refs, refname, repo);
780 if (error)
781 goto done;
784 if (argc == 0) {
785 error = got_ref_list(&include_refs, repo, "",
786 got_ref_cmp_by_name, NULL);
787 if (error)
788 goto done;
789 } else {
790 for (i = 0; i < argc; i++) {
791 const char *refname;
792 got_path_strip_trailing_slashes(argv[i]);
793 refname = argv[i];
794 error = add_ref(&new, &include_refs, refname, repo);
795 if (error)
796 goto done;
800 /* Ignore references in the refs/got/ namespace. */
801 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
802 const char *refname = got_ref_get_name(re->ref);
803 if (strncmp("refs/got/", refname, 9) != 0)
804 continue;
805 TAILQ_REMOVE(&include_refs, re, entry);
806 got_ref_close(re->ref);
807 free(re);
810 memset(&ppa, 0, sizeof(ppa));
811 ppa.out = stdout;
812 ppa.last_scaled_size[0] = '\0';
813 ppa.last_p_indexed = -1;
814 ppa.last_p_resolved = -1;
815 ppa.verbosity = verbosity;
817 error = got_repo_pack_objects(&packfile, &pack_hash,
818 &include_refs, &exclude_refs, repo, loose_obj_only,
819 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
820 if (error) {
821 if (ppa.printed_something)
822 printf("\n");
823 goto done;
826 error = got_object_id_str(&id_str, pack_hash);
827 if (error)
828 goto done;
829 if (verbosity >= 0)
830 printf("\nWrote %s.pack\n", id_str);
832 error = got_repo_index_pack(packfile, pack_hash, repo,
833 pack_index_progress, &ppa, check_cancelled, NULL);
834 if (error)
835 goto done;
836 if (verbosity >= 0)
837 printf("\nIndexed %s.pack\n", id_str);
838 done:
839 if (repo)
840 got_repo_close(repo);
841 if (pack_fds) {
842 const struct got_error *pack_err =
843 got_repo_pack_fds_close(pack_fds);
844 if (error == NULL)
845 error = pack_err;
847 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
848 got_ref_list_free(&exclude_refs);
849 got_ref_list_free(&include_refs);
850 free(id_str);
851 free(pack_hash);
852 free(repo_path);
853 return error;
856 __dead static void
857 usage_indexpack(void)
859 fprintf(stderr, "usage: %s indexpack packfile-path\n",
860 getprogname());
861 exit(1);
864 static const struct got_error *
865 cmd_indexpack(int argc, char *argv[])
867 const struct got_error *error = NULL;
868 struct got_repository *repo = NULL;
869 int ch;
870 struct got_object_id *pack_hash = NULL;
871 char *packfile_path = NULL;
872 char *id_str = NULL;
873 struct got_pack_progress_arg ppa;
874 FILE *packfile = NULL;
875 int *pack_fds = NULL;
877 while ((ch = getopt(argc, argv, "")) != -1) {
878 switch (ch) {
879 default:
880 usage_indexpack();
881 /* NOTREACHED */
885 argc -= optind;
886 argv += optind;
888 if (argc != 1)
889 usage_indexpack();
891 packfile_path = realpath(argv[0], NULL);
892 if (packfile_path == NULL)
893 return got_error_from_errno2("realpath", argv[0]);
895 #ifndef PROFILE
896 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
897 NULL) == -1)
898 err(1, "pledge");
899 #endif
901 error = got_repo_pack_fds_open(&pack_fds);
902 if (error != NULL)
903 goto done;
904 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
905 if (error)
906 goto done;
908 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
909 if (error)
910 goto done;
912 memset(&ppa, 0, sizeof(ppa));
913 ppa.out = stdout;
914 ppa.last_scaled_size[0] = '\0';
915 ppa.last_p_indexed = -1;
916 ppa.last_p_resolved = -1;
918 error = got_repo_find_pack(&packfile, &pack_hash, repo,
919 packfile_path);
920 if (error)
921 goto done;
923 error = got_object_id_str(&id_str, pack_hash);
924 if (error)
925 goto done;
927 error = got_repo_index_pack(packfile, pack_hash, repo,
928 pack_index_progress, &ppa, check_cancelled, NULL);
929 if (error)
930 goto done;
931 printf("\nIndexed %s.pack\n", id_str);
932 done:
933 if (repo)
934 got_repo_close(repo);
935 if (pack_fds) {
936 const struct got_error *pack_err =
937 got_repo_pack_fds_close(pack_fds);
938 if (error == NULL)
939 error = pack_err;
941 free(id_str);
942 free(pack_hash);
943 return error;
946 __dead static void
947 usage_listpack(void)
949 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
950 getprogname());
951 exit(1);
954 struct gotadmin_list_pack_cb_args {
955 int nblobs;
956 int ntrees;
957 int ncommits;
958 int ntags;
959 int noffdeltas;
960 int nrefdeltas;
961 int human_readable;
962 };
964 static const struct got_error *
965 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
966 off_t size, off_t base_offset, struct got_object_id *base_id)
968 const struct got_error *err;
969 struct gotadmin_list_pack_cb_args *a = arg;
970 char *id_str, *delta_str = NULL, *base_id_str = NULL;
971 const char *type_str;
973 err = got_object_id_str(&id_str, id);
974 if (err)
975 return err;
977 switch (type) {
978 case GOT_OBJ_TYPE_BLOB:
979 type_str = GOT_OBJ_LABEL_BLOB;
980 a->nblobs++;
981 break;
982 case GOT_OBJ_TYPE_TREE:
983 type_str = GOT_OBJ_LABEL_TREE;
984 a->ntrees++;
985 break;
986 case GOT_OBJ_TYPE_COMMIT:
987 type_str = GOT_OBJ_LABEL_COMMIT;
988 a->ncommits++;
989 break;
990 case GOT_OBJ_TYPE_TAG:
991 type_str = GOT_OBJ_LABEL_TAG;
992 a->ntags++;
993 break;
994 case GOT_OBJ_TYPE_OFFSET_DELTA:
995 type_str = "offset-delta";
996 if (asprintf(&delta_str, " base-offset %lld",
997 (long long)base_offset) == -1) {
998 err = got_error_from_errno("asprintf");
999 goto done;
1001 a->noffdeltas++;
1002 break;
1003 case GOT_OBJ_TYPE_REF_DELTA:
1004 type_str = "ref-delta";
1005 err = got_object_id_str(&base_id_str, base_id);
1006 if (err)
1007 goto done;
1008 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1009 err = got_error_from_errno("asprintf");
1010 goto done;
1012 a->nrefdeltas++;
1013 break;
1014 default:
1015 err = got_error(GOT_ERR_OBJ_TYPE);
1016 goto done;
1018 if (a->human_readable) {
1019 char scaled[FMT_SCALED_STRSIZE];
1020 char *s;;
1021 if (fmt_scaled(size, scaled) == -1) {
1022 err = got_error_from_errno("fmt_scaled");
1023 goto done;
1025 s = scaled;
1026 while (isspace((unsigned char)*s))
1027 s++;
1028 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1029 (long long)offset, s, delta_str ? delta_str : "");
1030 } else {
1031 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1032 (long long)offset, (long long)size,
1033 delta_str ? delta_str : "");
1035 done:
1036 free(id_str);
1037 free(base_id_str);
1038 free(delta_str);
1039 return err;
1042 static const struct got_error *
1043 cmd_listpack(int argc, char *argv[])
1045 const struct got_error *error = NULL;
1046 struct got_repository *repo = NULL;
1047 int ch;
1048 struct got_object_id *pack_hash = NULL;
1049 char *packfile_path = NULL;
1050 char *id_str = NULL;
1051 struct gotadmin_list_pack_cb_args lpa;
1052 FILE *packfile = NULL;
1053 int show_stats = 0, human_readable = 0;
1054 int *pack_fds = NULL;
1056 #ifndef PROFILE
1057 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1058 NULL) == -1)
1059 err(1, "pledge");
1060 #endif
1062 while ((ch = getopt(argc, argv, "hs")) != -1) {
1063 switch (ch) {
1064 case 'h':
1065 human_readable = 1;
1066 break;
1067 case 's':
1068 show_stats = 1;
1069 break;
1070 default:
1071 usage_listpack();
1072 /* NOTREACHED */
1076 argc -= optind;
1077 argv += optind;
1079 if (argc != 1)
1080 usage_listpack();
1081 packfile_path = realpath(argv[0], NULL);
1082 if (packfile_path == NULL)
1083 return got_error_from_errno2("realpath", argv[0]);
1085 error = got_repo_pack_fds_open(&pack_fds);
1086 if (error != NULL)
1087 goto done;
1088 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1089 if (error)
1090 goto done;
1091 #ifndef PROFILE
1092 /* Remove "cpath" promise. */
1093 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1094 NULL) == -1)
1095 err(1, "pledge");
1096 #endif
1097 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1098 if (error)
1099 goto done;
1101 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1102 packfile_path);
1103 if (error)
1104 goto done;
1105 error = got_object_id_str(&id_str, pack_hash);
1106 if (error)
1107 goto done;
1109 memset(&lpa, 0, sizeof(lpa));
1110 lpa.human_readable = human_readable;
1111 error = got_repo_list_pack(packfile, pack_hash, repo,
1112 list_pack_cb, &lpa, check_cancelled, NULL);
1113 if (error)
1114 goto done;
1115 if (show_stats) {
1116 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1117 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1118 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1119 lpa.noffdeltas + lpa.nrefdeltas,
1120 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1121 lpa.noffdeltas, lpa.nrefdeltas);
1123 done:
1124 if (repo)
1125 got_repo_close(repo);
1126 if (pack_fds) {
1127 const struct got_error *pack_err =
1128 got_repo_pack_fds_close(pack_fds);
1129 if (error == NULL)
1130 error = pack_err;
1132 free(id_str);
1133 free(pack_hash);
1134 free(packfile_path);
1135 return error;
1138 __dead static void
1139 usage_cleanup(void)
1141 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1142 getprogname());
1143 exit(1);
1146 struct got_cleanup_progress_arg {
1147 int last_nloose;
1148 int last_ncommits;
1149 int last_npurged;
1150 int last_nredundant;
1151 int verbosity;
1152 int printed_something;
1153 int dry_run;
1156 static const struct got_error *
1157 cleanup_progress(void *arg, int ncommits, int nloose, int npurged,
1158 int nredundant)
1160 struct got_cleanup_progress_arg *a = arg;
1161 int print_loose = 0, print_commits = 0, print_purged = 0;
1162 int print_redundant = 0;
1164 if (a->last_ncommits != ncommits) {
1165 print_commits = 1;
1166 a->last_ncommits = ncommits;
1168 if (a->last_nloose != nloose) {
1169 print_commits = 1;
1170 print_loose = 1;
1171 a->last_nloose = nloose;
1173 if (a->last_npurged != npurged) {
1174 print_commits = 1;
1175 print_loose = 1;
1176 print_purged = 1;
1177 a->last_npurged = npurged;
1179 if (a->last_nredundant != nredundant) {
1180 print_commits = 1;
1181 print_loose = 1;
1182 print_purged = 1;
1183 print_redundant = 1;
1184 a->last_nredundant = nredundant;
1187 if (a->verbosity < 0)
1188 return NULL;
1190 if (print_loose || print_commits || print_purged || print_redundant)
1191 printf("\r");
1192 if (print_commits)
1193 printf("%d commit%s scanned", ncommits,
1194 ncommits == 1 ? "" : "s");
1195 if (print_loose)
1196 printf("; %d loose object%s", nloose, nloose == 1 ? "" : "s");
1197 if (print_purged || print_redundant) {
1198 if (a->dry_run) {
1199 printf("; could purge %d object%s", npurged,
1200 npurged == 1 ? "" : "s");
1201 } else {
1202 printf("; purged %d object%s", npurged,
1203 npurged == 1 ? "" : "s");
1206 if (print_redundant) {
1207 if (a->dry_run) {
1208 printf(", %d pack file%s", nredundant,
1209 nredundant == 1 ? "" : "s");
1210 } else {
1211 printf(", %d pack file%s", nredundant,
1212 nredundant == 1 ? "" : "s");
1215 if (print_loose || print_commits || print_purged || print_redundant) {
1216 a->printed_something = 1;
1217 fflush(stdout);
1219 return NULL;
1222 struct got_lonely_packidx_progress_arg {
1223 int verbosity;
1224 int printed_something;
1225 int dry_run;
1228 static const struct got_error *
1229 lonely_packidx_progress(void *arg, const char *path)
1231 struct got_lonely_packidx_progress_arg *a = arg;
1233 if (a->verbosity < 0)
1234 return NULL;
1236 if (a->dry_run)
1237 printf("%s could be removed\n", path);
1238 else
1239 printf("%s removed\n", path);
1241 a->printed_something = 1;
1242 return NULL;
1245 static const struct got_error *
1246 cmd_cleanup(int argc, char *argv[])
1248 const struct got_error *error = NULL;
1249 char *repo_path = NULL;
1250 struct got_repository *repo = NULL;
1251 int ch, dry_run = 0, verbosity = 0;
1252 int ncommits = 0, nloose = 0, npacked = 0;
1253 int remove_lonely_packidx = 0, ignore_mtime = 0;
1254 struct got_cleanup_progress_arg cpa;
1255 struct got_lonely_packidx_progress_arg lpa;
1256 off_t loose_before, loose_after;
1257 off_t pack_before, pack_after;
1258 off_t total_size;
1259 char loose_before_scaled[FMT_SCALED_STRSIZE];
1260 char loose_after_scaled[FMT_SCALED_STRSIZE];
1261 char pack_before_scaled[FMT_SCALED_STRSIZE];
1262 char pack_after_scaled[FMT_SCALED_STRSIZE];
1263 char total_size_scaled[FMT_SCALED_STRSIZE];
1264 int *pack_fds = NULL;
1266 #ifndef PROFILE
1267 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1268 NULL) == -1)
1269 err(1, "pledge");
1270 #endif
1272 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1273 switch (ch) {
1274 case 'a':
1275 ignore_mtime = 1;
1276 break;
1277 case 'n':
1278 dry_run = 1;
1279 break;
1280 case 'p':
1281 remove_lonely_packidx = 1;
1282 break;
1283 case 'q':
1284 verbosity = -1;
1285 break;
1286 case 'r':
1287 repo_path = realpath(optarg, NULL);
1288 if (repo_path == NULL)
1289 return got_error_from_errno2("realpath",
1290 optarg);
1291 got_path_strip_trailing_slashes(repo_path);
1292 break;
1293 default:
1294 usage_cleanup();
1295 /* NOTREACHED */
1299 argc -= optind;
1300 argv += optind;
1302 if (repo_path == NULL) {
1303 error = get_repo_path(&repo_path);
1304 if (error)
1305 goto done;
1307 error = got_repo_pack_fds_open(&pack_fds);
1308 if (error != NULL)
1309 goto done;
1310 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1311 if (error)
1312 goto done;
1314 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1315 if (error)
1316 goto done;
1318 if (got_repo_has_extension(repo, "preciousObjects")) {
1319 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1320 "the preciousObjects Git extension is enabled; "
1321 "this implies that objects must not be deleted");
1322 goto done;
1325 if (remove_lonely_packidx) {
1326 memset(&lpa, 0, sizeof(lpa));
1327 lpa.dry_run = dry_run;
1328 lpa.verbosity = verbosity;
1329 error = got_repo_remove_lonely_packidx(repo, dry_run,
1330 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1331 goto done;
1334 memset(&cpa, 0, sizeof(cpa));
1335 cpa.last_nloose = -1;
1336 cpa.last_npurged = -1;
1337 cpa.last_nredundant = -1;
1338 cpa.dry_run = dry_run;
1339 cpa.verbosity = verbosity;
1341 error = got_repo_cleanup(repo, &loose_before, &loose_after,
1342 &pack_before, &pack_after, &ncommits, &nloose, &npacked,
1343 dry_run, ignore_mtime, cleanup_progress, &cpa,
1344 check_cancelled, NULL);
1345 if (cpa.printed_something)
1346 printf("\n");
1347 if (error)
1348 goto done;
1350 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1352 if (cpa.printed_something) {
1353 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1354 error = got_error_from_errno("fmt_scaled");
1355 goto done;
1357 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1358 error = got_error_from_errno("fmt_scaled");
1359 goto done;
1361 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1362 error = got_error_from_errno("fmt_scaled");
1363 goto done;
1365 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1366 error = got_error_from_errno("fmt_scaled");
1367 goto done;
1369 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1370 error = got_error_from_errno("fmt_scaled");
1371 goto done;
1373 printf("loose total size before: %s\n", loose_before_scaled);
1374 printf("loose total size after: %s\n", loose_after_scaled);
1375 printf("pack files total size before: %s\n",
1376 pack_before_scaled);
1377 printf("pack files total size after: %s\n", pack_after_scaled);
1378 if (dry_run) {
1379 printf("disk space which would be freed: %s\n",
1380 total_size_scaled);
1381 } else
1382 printf("disk space freed: %s\n", total_size_scaled);
1383 printf("loose objects also found in pack files: %d\n", npacked);
1386 done:
1387 if (repo)
1388 got_repo_close(repo);
1389 if (pack_fds) {
1390 const struct got_error *pack_err =
1391 got_repo_pack_fds_close(pack_fds);
1392 if (error == NULL)
1393 error = pack_err;
1395 free(repo_path);
1396 return error;
1399 __dead static void
1400 usage_dump(void)
1402 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1403 "[-x reference] [reference]...\n", getprogname());
1404 exit(1);
1407 static const struct got_error *
1408 cmd_dump(int argc, char *argv[])
1410 const struct got_error *error = NULL;
1411 struct got_pack_progress_arg ppa;
1412 struct got_repository *repo = NULL;
1413 struct got_pathlist_head exclude_args;
1414 struct got_pathlist_entry *pe;
1415 struct got_reflist_head exclude_refs;
1416 struct got_reflist_head include_refs;
1417 struct got_reflist_entry *re, *new;
1418 const char *refname;
1419 char *repo_path = NULL;
1420 int *pack_fds = NULL;
1421 int verbosity = 0;
1422 int i, ch;
1424 TAILQ_INIT(&exclude_args);
1425 TAILQ_INIT(&exclude_refs);
1426 TAILQ_INIT(&include_refs);
1428 #ifndef PROFILE
1429 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1430 NULL) == -1)
1431 err(1, "pledge");
1432 #endif
1434 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1435 switch (ch) {
1436 case 'q':
1437 verbosity = -1;
1438 break;
1439 case 'r':
1440 repo_path = realpath(optarg, NULL);
1441 if (repo_path == NULL)
1442 return got_error_from_errno2("realpath",
1443 optarg);
1444 got_path_strip_trailing_slashes(repo_path);
1445 break;
1446 case 'x':
1447 error = got_pathlist_append(&exclude_args,
1448 optarg, NULL);
1449 if (error)
1450 return error;
1451 break;
1452 default:
1453 usage_dump();
1454 /* NOTREACHED */
1457 argc -= optind;
1458 argv += optind;
1460 if (repo_path == NULL) {
1461 error = get_repo_path(&repo_path);
1462 if (error)
1463 goto done;
1465 error = got_repo_pack_fds_open(&pack_fds);
1466 if (error != NULL)
1467 goto done;
1468 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1469 if (error)
1470 goto done;
1472 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1473 if (error)
1474 goto done;
1476 TAILQ_FOREACH(pe, &exclude_args, entry) {
1477 refname = pe->path;
1478 error = add_ref(&new, &exclude_refs, refname, repo);
1479 if (error)
1480 goto done;
1483 if (argc == 0) {
1484 error = got_ref_list(&include_refs, repo, "",
1485 got_ref_cmp_by_name, NULL);
1486 if (error)
1487 goto done;
1488 } else {
1489 for (i = 0; i < argc; i++) {
1490 got_path_strip_trailing_slashes(argv[i]);
1491 refname = argv[i];
1492 error = add_ref(&new, &include_refs, refname, repo);
1493 if (error)
1494 goto done;
1498 /* Ignore references in the refs/got/ namespace. */
1499 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1500 refname = got_ref_get_name(re->ref);
1501 if (strncmp("refs/got/", refname, 9) != 0)
1502 continue;
1503 TAILQ_REMOVE(&include_refs, re, entry);
1504 got_ref_close(re->ref);
1505 free(re);
1508 memset(&ppa, 0, sizeof(ppa));
1509 ppa.out = stderr;
1510 ppa.verbosity = verbosity;
1512 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1513 repo, pack_progress, &ppa, check_cancelled, NULL);
1514 if (ppa.printed_something)
1515 fprintf(stderr, "\n");
1516 done:
1517 if (repo)
1518 got_repo_close(repo);
1520 if (pack_fds) {
1521 const struct got_error *pack_err;
1523 pack_err = got_repo_pack_fds_close(pack_fds);
1524 if (error == NULL)
1525 error = pack_err;
1528 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1529 got_ref_list_free(&exclude_refs);
1530 got_ref_list_free(&include_refs);
1531 free(repo_path);
1533 return error;
1536 __dead static void
1537 usage_load(void)
1539 fprintf(stderr, "usage: %s load [-nq] [-l bundle-file] "
1540 "[-r repository-path] [reference ...]\n",
1541 getprogname());
1542 exit(1);
1545 static const struct got_error *
1546 load_progress(void *arg, off_t packfile_size, int nobj_total,
1547 int nobj_indexed, int nobj_loose, int nobj_resolved)
1549 return pack_index_progress(arg, packfile_size, nobj_total,
1550 nobj_indexed, nobj_loose, nobj_resolved);
1553 static int
1554 is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1556 struct got_pathlist_entry *pe;
1558 if (TAILQ_EMPTY(wanted))
1559 return 1;
1561 TAILQ_FOREACH(pe, wanted, entry) {
1562 if (strcmp(pe->path, ref) == 0)
1563 return 1;
1566 return 0;
1569 static const struct got_error *
1570 create_ref(const char *refname, struct got_object_id *id,
1571 int verbosity, struct got_repository *repo)
1573 const struct got_error *err = NULL;
1574 struct got_reference *ref;
1575 char *id_str;
1577 err = got_object_id_str(&id_str, id);
1578 if (err)
1579 return err;
1581 err = got_ref_alloc(&ref, refname, id);
1582 if (err)
1583 goto done;
1585 err = got_ref_write(ref, repo);
1586 got_ref_close(ref);
1588 if (err == NULL && verbosity >= 0)
1589 printf("Created reference %s: %s\n", refname, id_str);
1590 done:
1591 free(id_str);
1592 return err;
1595 static const struct got_error *
1596 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1597 int replace_tags, int verbosity, struct got_repository *repo)
1599 const struct got_error *err = NULL;
1600 char *new_id_str = NULL;
1601 struct got_object_id *old_id = NULL;
1603 err = got_object_id_str(&new_id_str, new_id);
1604 if (err)
1605 goto done;
1607 if (!replace_tags &&
1608 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1609 err = got_ref_resolve(&old_id, repo, ref);
1610 if (err)
1611 goto done;
1612 if (got_object_id_cmp(old_id, new_id) == 0)
1613 goto done;
1614 if (verbosity >= 0) {
1615 printf("Rejecting update of existing tag %s: %s\n",
1616 got_ref_get_name(ref), new_id_str);
1618 goto done;
1621 if (got_ref_is_symbolic(ref)) {
1622 if (verbosity >= 0) {
1623 printf("Replacing reference %s: %s\n",
1624 got_ref_get_name(ref),
1625 got_ref_get_symref_target(ref));
1627 err = got_ref_change_symref_to_ref(ref, new_id);
1628 if (err)
1629 goto done;
1630 err = got_ref_write(ref, repo);
1631 if (err)
1632 goto done;
1633 } else {
1634 err = got_ref_resolve(&old_id, repo, ref);
1635 if (err)
1636 goto done;
1637 if (got_object_id_cmp(old_id, new_id) == 0)
1638 goto done;
1640 err = got_ref_change_ref(ref, new_id);
1641 if (err)
1642 goto done;
1643 err = got_ref_write(ref, repo);
1644 if (err)
1645 goto done;
1648 if (verbosity >= 0)
1649 printf("Updated %s: %s\n", got_ref_get_name(ref),
1650 new_id_str);
1651 done:
1652 free(old_id);
1653 free(new_id_str);
1654 return err;
1657 static const struct got_error *
1658 cmd_load(int argc, char *argv[])
1660 const struct got_error *error = NULL;
1661 struct got_repository *repo = NULL;
1662 struct got_pathlist_head include_args;
1663 struct got_pathlist_head available_refs;
1664 struct got_pathlist_entry *pe;
1665 struct got_pack_progress_arg ppa;
1666 FILE *in = stdin;
1667 int *pack_fds = NULL;
1668 char *repo_path = NULL;
1669 int list_refs_only = 0;
1670 int noop = 0;
1671 int verbosity = 0;
1672 int ch, i;
1674 TAILQ_INIT(&include_args);
1675 TAILQ_INIT(&available_refs);
1677 #ifndef PROFILE
1678 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1679 "sendfd unveil", NULL) == -1)
1680 err(1, "pledge");
1681 #endif
1683 while ((ch = getopt(argc, argv, "l:nqr:")) != -1) {
1684 switch (ch) {
1685 case 'l':
1686 list_refs_only = 1;
1687 in = fopen(optarg, "re");
1688 if (in == NULL)
1689 return got_error_from_errno2("open", optarg);
1690 break;
1691 case 'n':
1692 noop = 1;
1693 break;
1694 case 'q':
1695 verbosity = -1;
1696 break;
1697 case 'r':
1698 repo_path = realpath(optarg, NULL);
1699 if (repo_path == NULL)
1700 return got_error_from_errno2("realpath",
1701 optarg);
1702 got_path_strip_trailing_slashes(repo_path);
1703 break;
1704 default:
1705 usage_load();
1706 /* NOTREACHED */
1709 argc -= optind;
1710 argv += optind;
1712 if (list_refs_only && argc > 1)
1713 errx(1, "-l and references on the command line are exclusive");
1714 if (list_refs_only && noop)
1715 errx(1, "-n and -l are mutually exclusive");
1717 for (i = 0; i < argc; i++) {
1718 char *refname = argv[i];
1719 got_path_strip_trailing_slashes(refname);
1720 if (!got_ref_name_is_valid(refname))
1721 errx(1, "invalid reference name %s", refname);
1722 error = got_pathlist_append(&include_args, refname, NULL);
1723 if (error)
1724 goto done;
1727 if (repo_path == NULL) {
1728 error = get_repo_path(&repo_path);
1729 if (error)
1730 goto done;
1732 error = got_repo_pack_fds_open(&pack_fds);
1733 if (error != NULL)
1734 goto done;
1735 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1736 if (error)
1737 goto done;
1739 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1740 if (error)
1741 goto done;
1743 memset(&ppa, 0, sizeof(ppa));
1744 ppa.out = stdout;
1745 ppa.verbosity = verbosity;
1747 error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1748 load_progress, &ppa, check_cancelled, NULL);
1749 if (verbosity >= 0 && !list_refs_only)
1750 printf("\n");
1751 if (error)
1752 goto done;
1754 if (list_refs_only) {
1755 TAILQ_FOREACH(pe, &available_refs, entry) {
1756 const char *refname = pe->path;
1757 struct got_object_id *id = pe->data;
1758 char *idstr;
1760 error = got_object_id_str(&idstr, id);
1761 if (error)
1762 goto done;
1764 printf("%s: %s\n", refname, idstr);
1765 free(idstr);
1767 goto done;
1770 if (noop)
1771 goto done;
1773 /* Update references */
1774 TAILQ_FOREACH(pe, &available_refs, entry) {
1775 const struct got_error *unlock_err;
1776 struct got_reference *ref;
1777 const char *refname = pe->path;
1778 struct got_object_id *id = pe->data;
1780 if (!is_wanted_ref(&include_args, pe->path))
1781 continue;
1783 error = got_ref_open(&ref, repo, refname, 1);
1784 if (error) {
1785 if (error->code != GOT_ERR_NOT_REF)
1786 goto done;
1787 error = create_ref(refname, id, verbosity, repo);
1788 if (error)
1789 goto done;
1790 } else {
1791 /* XXX: check advances only and add -f to force? */
1792 error = update_ref(ref, id, 1, verbosity, repo);
1793 unlock_err = got_ref_unlock(ref);
1794 if (unlock_err && error == NULL)
1795 error = unlock_err;
1796 got_ref_close(ref);
1797 if (error)
1798 goto done;
1802 done:
1803 if (in != stdin && fclose(in) == EOF && error == NULL)
1804 error = got_error_from_errno("fclose");
1806 if (repo)
1807 got_repo_close(repo);
1809 if (pack_fds) {
1810 const struct got_error *pack_err;
1812 pack_err = got_repo_pack_fds_close(pack_fds);
1813 if (error == NULL)
1814 error = pack_err;
1817 got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1818 got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);
1819 free(repo_path);
1821 return error;