2 20662ea0 2021-04-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 20662ea0 2021-04-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 20662ea0 2021-04-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 20662ea0 2021-04-10 stsp * copyright notice and this permission notice appear in all copies.
8 20662ea0 2021-04-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 20662ea0 2021-04-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 20662ea0 2021-04-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 20662ea0 2021-04-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 20662ea0 2021-04-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 20662ea0 2021-04-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 20662ea0 2021-04-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 05118f5a 2021-06-22 stsp #include <sys/types.h>
22 05118f5a 2021-06-22 stsp #include <ctype.h>
23 20662ea0 2021-04-10 stsp #include <getopt.h>
24 20662ea0 2021-04-10 stsp #include <err.h>
25 20662ea0 2021-04-10 stsp #include <errno.h>
26 20662ea0 2021-04-10 stsp #include <locale.h>
27 05118f5a 2021-06-22 stsp #include <inttypes.h>
28 20662ea0 2021-04-10 stsp #include <stdio.h>
29 20662ea0 2021-04-10 stsp #include <stdlib.h>
30 20662ea0 2021-04-10 stsp #include <signal.h>
31 20662ea0 2021-04-10 stsp #include <string.h>
32 20662ea0 2021-04-10 stsp #include <unistd.h>
34 20662ea0 2021-04-10 stsp #include "got_version.h"
35 20662ea0 2021-04-10 stsp #include "got_error.h"
36 20662ea0 2021-04-10 stsp #include "got_object.h"
37 20662ea0 2021-04-10 stsp #include "got_reference.h"
38 05118f5a 2021-06-22 stsp #include "got_cancel.h"
39 20662ea0 2021-04-10 stsp #include "got_repository.h"
40 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
41 fdbea373 2023-07-10 thomas #include "got_repository_dump.h"
42 90afc9f3 2023-07-10 thomas #include "got_repository_load.h"
43 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
44 20662ea0 2021-04-10 stsp #include "got_path.h"
45 20662ea0 2021-04-10 stsp #include "got_privsep.h"
46 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
47 1ea7ccc6 2021-11-15 thomas #include "got_worktree.h"
49 20662ea0 2021-04-10 stsp #ifndef nitems
50 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
54 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
57 20662ea0 2021-04-10 stsp catch_sigint(int signo)
59 20662ea0 2021-04-10 stsp sigint_received = 1;
63 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
65 20662ea0 2021-04-10 stsp sigpipe_received = 1;
68 05118f5a 2021-06-22 stsp static const struct got_error *
69 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
71 05118f5a 2021-06-22 stsp if (sigint_received || sigpipe_received)
72 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_CANCELLED);
73 05118f5a 2021-06-22 stsp return NULL;
76 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
77 20662ea0 2021-04-10 stsp const char *cmd_name;
78 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
79 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
80 20662ea0 2021-04-10 stsp const char *cmd_alias;
83 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
84 27b10c3c 2022-07-04 thomas __dead static void usage_init(void);
85 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
86 05118f5a 2021-06-22 stsp __dead static void usage_pack(void);
87 05118f5a 2021-06-22 stsp __dead static void usage_indexpack(void);
88 05118f5a 2021-06-22 stsp __dead static void usage_listpack(void);
89 b3d68e7f 2021-07-03 stsp __dead static void usage_cleanup(void);
90 fdbea373 2023-07-10 thomas __dead static void usage_dump(void);
91 90afc9f3 2023-07-10 thomas __dead static void usage_load(void);
93 27b10c3c 2022-07-04 thomas static const struct got_error* cmd_init(int, char *[]);
94 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
95 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
96 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
97 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
98 b3d68e7f 2021-07-03 stsp static const struct got_error* cmd_cleanup(int, char *[]);
99 fdbea373 2023-07-10 thomas static const struct got_error* cmd_dump(int, char *[]);
100 90afc9f3 2023-07-10 thomas static const struct got_error* cmd_load(int, char *[]);
102 641a8ee6 2022-02-16 thomas static const struct gotadmin_cmd gotadmin_commands[] = {
103 27b10c3c 2022-07-04 thomas { "init", cmd_init, usage_init, "" },
104 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
105 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
106 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
107 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
108 b3d68e7f 2021-07-03 stsp { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
109 fdbea373 2023-07-10 thomas { "dump", cmd_dump, usage_dump, "" },
110 90afc9f3 2023-07-10 thomas { "load", cmd_load, usage_load, "" },
113 20662ea0 2021-04-10 stsp static void
114 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
118 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
119 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
120 641a8ee6 2022-02-16 thomas const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
121 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
123 20662ea0 2021-04-10 stsp fputc('\n', fp);
127 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
129 641a8ee6 2022-02-16 thomas const struct gotadmin_cmd *cmd;
132 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
133 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
134 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
135 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
138 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
140 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
141 20662ea0 2021-04-10 stsp switch (ch) {
149 20662ea0 2021-04-10 stsp usage(hflag, 1);
150 20662ea0 2021-04-10 stsp /* NOTREACHED */
154 20662ea0 2021-04-10 stsp argc -= optind;
155 20662ea0 2021-04-10 stsp argv += optind;
156 20662ea0 2021-04-10 stsp optind = 1;
157 20662ea0 2021-04-10 stsp optreset = 1;
159 20662ea0 2021-04-10 stsp if (Vflag) {
160 20662ea0 2021-04-10 stsp got_version_print_str();
164 20662ea0 2021-04-10 stsp if (argc <= 0)
165 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
167 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
168 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
170 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
171 20662ea0 2021-04-10 stsp const struct got_error *error;
173 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
175 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
176 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
180 641a8ee6 2022-02-16 thomas cmd->cmd_usage();
182 641a8ee6 2022-02-16 thomas error = cmd->cmd_main(argc, argv);
183 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
184 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
185 20662ea0 2021-04-10 stsp !(sigpipe_received &&
186 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
187 20662ea0 2021-04-10 stsp !(sigint_received &&
188 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
189 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
196 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
197 20662ea0 2021-04-10 stsp list_commands(stderr);
201 20662ea0 2021-04-10 stsp __dead static void
202 20662ea0 2021-04-10 stsp usage(int hflag, int status)
204 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
206 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
207 20662ea0 2021-04-10 stsp getprogname());
209 20662ea0 2021-04-10 stsp list_commands(fp);
210 20662ea0 2021-04-10 stsp exit(status);
213 20662ea0 2021-04-10 stsp static const struct got_error *
214 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
216 20662ea0 2021-04-10 stsp const struct got_error *err;
218 20662ea0 2021-04-10 stsp #ifdef PROFILE
219 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
220 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
222 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
223 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
225 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
226 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
228 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
229 20662ea0 2021-04-10 stsp if (err != NULL)
230 20662ea0 2021-04-10 stsp return err;
232 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
233 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
235 20662ea0 2021-04-10 stsp return NULL;
238 20662ea0 2021-04-10 stsp __dead static void
239 20662ea0 2021-04-10 stsp usage_info(void)
241 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
242 20662ea0 2021-04-10 stsp getprogname());
246 20662ea0 2021-04-10 stsp static const struct got_error *
247 1ea7ccc6 2021-11-15 thomas get_repo_path(char **repo_path)
249 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
250 1ea7ccc6 2021-11-15 thomas struct got_worktree *worktree = NULL;
251 1ea7ccc6 2021-11-15 thomas char *cwd;
253 1ea7ccc6 2021-11-15 thomas *repo_path = NULL;
255 1ea7ccc6 2021-11-15 thomas cwd = getcwd(NULL, 0);
256 1ea7ccc6 2021-11-15 thomas if (cwd == NULL)
257 1ea7ccc6 2021-11-15 thomas return got_error_from_errno("getcwd");
259 ad10f64e 2023-07-19 thomas err = got_worktree_open(&worktree, cwd, NULL);
260 1ea7ccc6 2021-11-15 thomas if (err) {
261 1ea7ccc6 2021-11-15 thomas if (err->code != GOT_ERR_NOT_WORKTREE)
262 1ea7ccc6 2021-11-15 thomas goto done;
263 1ea7ccc6 2021-11-15 thomas err = NULL;
266 1ea7ccc6 2021-11-15 thomas if (worktree)
267 1ea7ccc6 2021-11-15 thomas *repo_path = strdup(got_worktree_get_repo_path(worktree));
269 1ea7ccc6 2021-11-15 thomas *repo_path = strdup(cwd);
270 1ea7ccc6 2021-11-15 thomas if (*repo_path == NULL)
271 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("strdup");
273 1ea7ccc6 2021-11-15 thomas if (worktree)
274 1ea7ccc6 2021-11-15 thomas got_worktree_close(worktree);
275 1ea7ccc6 2021-11-15 thomas free(cwd);
276 1ea7ccc6 2021-11-15 thomas return err;
279 27b10c3c 2022-07-04 thomas __dead static void
280 27b10c3c 2022-07-04 thomas usage_init(void)
282 e9424ba1 2022-09-20 thomas fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
283 e9424ba1 2022-09-20 thomas getprogname());
287 27b10c3c 2022-07-04 thomas static const struct got_error *
288 27b10c3c 2022-07-04 thomas cmd_init(int argc, char *argv[])
290 27b10c3c 2022-07-04 thomas const struct got_error *error = NULL;
291 e9424ba1 2022-09-20 thomas const char *head_name = NULL;
292 27b10c3c 2022-07-04 thomas char *repo_path = NULL;
295 a0abeae5 2023-02-17 thomas #ifndef PROFILE
296 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
297 a0abeae5 2023-02-17 thomas err(1, "pledge");
300 e9424ba1 2022-09-20 thomas while ((ch = getopt(argc, argv, "b:")) != -1) {
301 27b10c3c 2022-07-04 thomas switch (ch) {
302 e9424ba1 2022-09-20 thomas case 'b':
303 e9424ba1 2022-09-20 thomas head_name = optarg;
306 27b10c3c 2022-07-04 thomas usage_init();
307 27b10c3c 2022-07-04 thomas /* NOTREACHED */
311 27b10c3c 2022-07-04 thomas argc -= optind;
312 27b10c3c 2022-07-04 thomas argv += optind;
314 27b10c3c 2022-07-04 thomas if (argc != 1)
315 27b10c3c 2022-07-04 thomas usage_init();
317 27b10c3c 2022-07-04 thomas repo_path = strdup(argv[0]);
318 27b10c3c 2022-07-04 thomas if (repo_path == NULL)
319 27b10c3c 2022-07-04 thomas return got_error_from_errno("strdup");
321 27b10c3c 2022-07-04 thomas got_path_strip_trailing_slashes(repo_path);
323 27b10c3c 2022-07-04 thomas error = got_path_mkdir(repo_path);
324 27b10c3c 2022-07-04 thomas if (error &&
325 27b10c3c 2022-07-04 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
326 27b10c3c 2022-07-04 thomas goto done;
328 27b10c3c 2022-07-04 thomas error = apply_unveil(repo_path, 0);
329 27b10c3c 2022-07-04 thomas if (error)
330 27b10c3c 2022-07-04 thomas goto done;
332 e9424ba1 2022-09-20 thomas error = got_repo_init(repo_path, head_name);
334 27b10c3c 2022-07-04 thomas free(repo_path);
335 27b10c3c 2022-07-04 thomas return error;
338 1ea7ccc6 2021-11-15 thomas static const struct got_error *
339 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
341 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
342 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
343 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
344 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
345 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
346 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
347 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
348 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
350 a0abeae5 2023-02-17 thomas #ifndef PROFILE
351 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
352 a0abeae5 2023-02-17 thomas NULL) == -1)
353 a0abeae5 2023-02-17 thomas err(1, "pledge");
356 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
357 20662ea0 2021-04-10 stsp switch (ch) {
359 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
360 20662ea0 2021-04-10 stsp if (repo_path == NULL)
361 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
363 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
366 20662ea0 2021-04-10 stsp usage_info();
367 20662ea0 2021-04-10 stsp /* NOTREACHED */
371 20662ea0 2021-04-10 stsp argc -= optind;
372 20662ea0 2021-04-10 stsp argv += optind;
374 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
375 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
376 1ea7ccc6 2021-11-15 thomas if (error)
377 1ea7ccc6 2021-11-15 thomas goto done;
379 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
380 7cd52833 2022-06-23 thomas if (error != NULL)
381 7cd52833 2022-06-23 thomas goto done;
382 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
385 bfb5ee0b 2022-05-31 thomas #ifndef PROFILE
386 bfb5ee0b 2022-05-31 thomas /* Remove "cpath" promise. */
387 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
388 bfb5ee0b 2022-05-31 thomas NULL) == -1)
389 bfb5ee0b 2022-05-31 thomas err(1, "pledge");
391 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
395 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
397 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
398 20662ea0 2021-04-10 stsp if (gotconfig) {
399 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
400 20662ea0 2021-04-10 stsp int i, nremotes;
401 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
402 20662ea0 2021-04-10 stsp printf("default author: %s\n",
403 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
405 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
406 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
407 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
408 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
409 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
410 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
411 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
413 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
414 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
415 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
416 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
421 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
422 20662ea0 2021-04-10 stsp &packsize, repo);
425 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
426 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
427 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
428 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
431 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
432 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
435 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
438 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
439 20662ea0 2021-04-10 stsp if (nobj > 0) {
440 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
441 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
444 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
448 20662ea0 2021-04-10 stsp got_repo_close(repo);
449 7cd52833 2022-06-23 thomas if (pack_fds) {
450 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
451 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
452 7cd52833 2022-06-23 thomas if (error == NULL)
453 7cd52833 2022-06-23 thomas error = pack_err;
456 1ea7ccc6 2021-11-15 thomas free(repo_path);
457 20662ea0 2021-04-10 stsp return error;
460 05118f5a 2021-06-22 stsp __dead static void
461 05118f5a 2021-06-22 stsp usage_pack(void)
463 d8253374 2023-02-17 thomas fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
464 d6506a3d 2022-08-16 thomas "[-x reference] [reference ...]\n", getprogname());
468 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
469 fdbea373 2023-07-10 thomas FILE *out;
470 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
471 85220b0e 2022-03-22 thomas int last_ncolored;
472 85220b0e 2022-03-22 thomas int last_nfound;
473 85220b0e 2022-03-22 thomas int last_ntrees;
474 85220b0e 2022-03-22 thomas int loading_done;
475 05118f5a 2021-06-22 stsp int last_ncommits;
476 05118f5a 2021-06-22 stsp int last_nobj_total;
477 05118f5a 2021-06-22 stsp int last_p_deltify;
478 05118f5a 2021-06-22 stsp int last_p_written;
479 05118f5a 2021-06-22 stsp int last_p_indexed;
480 05118f5a 2021-06-22 stsp int last_p_resolved;
481 05118f5a 2021-06-22 stsp int verbosity;
482 05118f5a 2021-06-22 stsp int printed_something;
485 85220b0e 2022-03-22 thomas static void
486 fdbea373 2023-07-10 thomas print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
487 85220b0e 2022-03-22 thomas int ncolored, int nfound, int ntrees)
489 85220b0e 2022-03-22 thomas if (print_colored) {
490 fdbea373 2023-07-10 thomas fprintf(out, "%d commit%s colored", ncolored,
491 85220b0e 2022-03-22 thomas ncolored == 1 ? "" : "s");
493 85220b0e 2022-03-22 thomas if (print_found) {
494 fdbea373 2023-07-10 thomas fprintf(out, "%s%d object%s found",
495 85220b0e 2022-03-22 thomas ncolored > 0 ? "; " : "",
496 85220b0e 2022-03-22 thomas nfound, nfound == 1 ? "" : "s");
498 85220b0e 2022-03-22 thomas if (print_trees) {
499 fdbea373 2023-07-10 thomas fprintf(out, "; %d tree%s scanned", ntrees,
500 85220b0e 2022-03-22 thomas ntrees == 1 ? "" : "s");
504 05118f5a 2021-06-22 stsp static const struct got_error *
505 85220b0e 2022-03-22 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
506 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
507 85220b0e 2022-03-22 thomas int nobj_written)
509 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
510 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
511 05118f5a 2021-06-22 stsp int p_deltify, p_written;
512 85220b0e 2022-03-22 thomas int print_colored = 0, print_found = 0, print_trees = 0;
513 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
514 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
516 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
517 85220b0e 2022-03-22 thomas return NULL;
519 85220b0e 2022-03-22 thomas if (a->last_ncolored != ncolored) {
520 85220b0e 2022-03-22 thomas print_colored = 1;
521 85220b0e 2022-03-22 thomas a->last_ncolored = ncolored;
524 85220b0e 2022-03-22 thomas if (a->last_nfound != nfound) {
525 85220b0e 2022-03-22 thomas print_colored = 1;
526 85220b0e 2022-03-22 thomas print_found = 1;
527 85220b0e 2022-03-22 thomas a->last_nfound = nfound;
530 85220b0e 2022-03-22 thomas if (a->last_ntrees != ntrees) {
531 85220b0e 2022-03-22 thomas print_colored = 1;
532 85220b0e 2022-03-22 thomas print_found = 1;
533 85220b0e 2022-03-22 thomas print_trees = 1;
534 85220b0e 2022-03-22 thomas a->last_ntrees = ntrees;
537 85220b0e 2022-03-22 thomas if ((print_colored || print_found || print_trees) &&
538 85220b0e 2022-03-22 thomas !a->loading_done) {
539 fdbea373 2023-07-10 thomas fprintf(a->out, "\r");
540 fdbea373 2023-07-10 thomas print_load_info(a->out, print_colored, print_found,
541 fdbea373 2023-07-10 thomas print_trees, ncolored, nfound, ntrees);
542 85220b0e 2022-03-22 thomas a->printed_something = 1;
543 fdbea373 2023-07-10 thomas fflush(a->out);
544 05118f5a 2021-06-22 stsp return NULL;
545 85220b0e 2022-03-22 thomas } else if (!a->loading_done) {
546 fdbea373 2023-07-10 thomas fprintf(a->out, "\r");
547 fdbea373 2023-07-10 thomas print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
548 fdbea373 2023-07-10 thomas fprintf(a->out, "\n");
549 85220b0e 2022-03-22 thomas a->loading_done = 1;
552 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
553 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
555 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
556 05118f5a 2021-06-22 stsp print_searching = 1;
557 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
560 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
561 05118f5a 2021-06-22 stsp print_searching = 1;
562 05118f5a 2021-06-22 stsp print_total = 1;
563 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
566 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
567 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
568 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
569 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
570 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
573 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
574 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
575 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
576 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
577 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
578 05118f5a 2021-06-22 stsp print_searching = 1;
579 05118f5a 2021-06-22 stsp print_total = 1;
580 05118f5a 2021-06-22 stsp print_deltify = 1;
583 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
584 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
585 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
586 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
587 05118f5a 2021-06-22 stsp print_searching = 1;
588 05118f5a 2021-06-22 stsp print_total = 1;
589 05118f5a 2021-06-22 stsp print_deltify = 1;
590 05118f5a 2021-06-22 stsp print_written = 1;
595 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
596 fdbea373 2023-07-10 thomas fprintf(a->out, "\r");
597 05118f5a 2021-06-22 stsp if (print_searching)
598 fdbea373 2023-07-10 thomas fprintf(a->out, "packing %d reference%s", ncommits,
599 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
600 05118f5a 2021-06-22 stsp if (print_total)
601 fdbea373 2023-07-10 thomas fprintf(a->out, "; %d object%s", nobj_total,
602 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
603 05118f5a 2021-06-22 stsp if (print_deltify)
604 fdbea373 2023-07-10 thomas fprintf(a->out, "; deltify: %d%%", p_deltify);
605 05118f5a 2021-06-22 stsp if (print_written)
606 fdbea373 2023-07-10 thomas fprintf(a->out, "; writing pack: %*s %d%%",
607 fdbea373 2023-07-10 thomas FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
608 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
609 05118f5a 2021-06-22 stsp print_written) {
610 05118f5a 2021-06-22 stsp a->printed_something = 1;
611 fdbea373 2023-07-10 thomas fflush(a->out);
613 05118f5a 2021-06-22 stsp return NULL;
616 05118f5a 2021-06-22 stsp static const struct got_error *
617 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
618 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
620 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
621 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
622 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
623 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
625 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
626 05118f5a 2021-06-22 stsp return NULL;
628 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
629 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
630 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
631 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
632 05118f5a 2021-06-22 stsp print_size = 1;
633 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
634 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
635 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
637 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
638 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
639 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
640 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
641 05118f5a 2021-06-22 stsp print_indexed = 1;
642 05118f5a 2021-06-22 stsp print_size = 1;
645 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
646 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
647 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
648 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
649 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
650 05118f5a 2021-06-22 stsp print_resolved = 1;
651 05118f5a 2021-06-22 stsp print_indexed = 1;
652 05118f5a 2021-06-22 stsp print_size = 1;
657 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
658 05118f5a 2021-06-22 stsp printf("\r");
659 05118f5a 2021-06-22 stsp if (print_size)
660 d2f35ef7 2022-02-13 thomas printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
661 05118f5a 2021-06-22 stsp if (print_indexed)
662 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
663 05118f5a 2021-06-22 stsp if (print_resolved)
664 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
665 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
666 05118f5a 2021-06-22 stsp fflush(stdout);
668 05118f5a 2021-06-22 stsp return NULL;
671 05118f5a 2021-06-22 stsp static const struct got_error *
672 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
673 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
675 05118f5a 2021-06-22 stsp const struct got_error *err;
676 05118f5a 2021-06-22 stsp struct got_reference *ref;
678 05118f5a 2021-06-22 stsp *new = NULL;
680 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
682 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
683 05118f5a 2021-06-22 stsp return err;
685 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
686 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
687 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
689 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
690 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
691 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
692 05118f5a 2021-06-22 stsp got_ref_close(ref);
695 05118f5a 2021-06-22 stsp return err;
698 05118f5a 2021-06-22 stsp static const struct got_error *
699 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
701 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
702 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
703 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
704 d8253374 2023-02-17 thomas int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
705 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
706 05118f5a 2021-06-22 stsp char *id_str = NULL;
707 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
708 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
709 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
710 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
711 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
712 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
713 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
714 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
716 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
717 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
718 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
720 a0abeae5 2023-02-17 thomas #ifndef PROFILE
721 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
722 a0abeae5 2023-02-17 thomas NULL) == -1)
723 a0abeae5 2023-02-17 thomas err(1, "pledge");
726 d8253374 2023-02-17 thomas while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
727 05118f5a 2021-06-22 stsp switch (ch) {
729 05118f5a 2021-06-22 stsp loose_obj_only = 0;
731 d8253374 2023-02-17 thomas case 'D':
732 d8253374 2023-02-17 thomas force_refdelta = 1;
734 f7065961 2022-10-27 thomas case 'q':
735 f7065961 2022-10-27 thomas verbosity = -1;
738 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
739 05118f5a 2021-06-22 stsp if (repo_path == NULL)
740 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
742 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
745 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
746 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
747 05118f5a 2021-06-22 stsp optarg, NULL);
749 05118f5a 2021-06-22 stsp return error;
752 05118f5a 2021-06-22 stsp usage_pack();
753 05118f5a 2021-06-22 stsp /* NOTREACHED */
757 05118f5a 2021-06-22 stsp argc -= optind;
758 05118f5a 2021-06-22 stsp argv += optind;
760 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
761 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
762 1ea7ccc6 2021-11-15 thomas if (error)
763 1ea7ccc6 2021-11-15 thomas goto done;
765 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
766 7cd52833 2022-06-23 thomas if (error != NULL)
767 7cd52833 2022-06-23 thomas goto done;
768 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
772 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
776 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
777 05118f5a 2021-06-22 stsp const char *refname = pe->path;
778 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
783 05118f5a 2021-06-22 stsp if (argc == 0) {
784 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
785 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
789 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
790 05118f5a 2021-06-22 stsp const char *refname;
791 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
792 05118f5a 2021-06-22 stsp refname = argv[i];
793 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
799 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
800 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
801 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
802 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
804 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
805 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
809 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
810 fdbea373 2023-07-10 thomas ppa.out = stdout;
811 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
812 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
813 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
814 c8fc6d14 2022-04-16 thomas ppa.verbosity = verbosity;
816 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
817 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
818 d8253374 2023-02-17 thomas force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
819 05118f5a 2021-06-22 stsp if (error) {
820 05118f5a 2021-06-22 stsp if (ppa.printed_something)
821 05118f5a 2021-06-22 stsp printf("\n");
825 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
828 c8fc6d14 2022-04-16 thomas if (verbosity >= 0)
829 c8fc6d14 2022-04-16 thomas printf("\nWrote %s.pack\n", id_str);
831 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
832 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
835 c8fc6d14 2022-04-16 thomas if (verbosity >= 0)
836 c8fc6d14 2022-04-16 thomas printf("\nIndexed %s.pack\n", id_str);
838 c75a6067 2021-10-15 thomas if (repo)
839 c75a6067 2021-10-15 thomas got_repo_close(repo);
840 7cd52833 2022-06-23 thomas if (pack_fds) {
841 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
842 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
843 7cd52833 2022-06-23 thomas if (error == NULL)
844 7cd52833 2022-06-23 thomas error = pack_err;
846 21c2d8be 2023-01-10 thomas got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
847 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
848 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
849 05118f5a 2021-06-22 stsp free(id_str);
850 05118f5a 2021-06-22 stsp free(pack_hash);
851 1ea7ccc6 2021-11-15 thomas free(repo_path);
852 05118f5a 2021-06-22 stsp return error;
855 05118f5a 2021-06-22 stsp __dead static void
856 05118f5a 2021-06-22 stsp usage_indexpack(void)
858 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
859 05118f5a 2021-06-22 stsp getprogname());
863 05118f5a 2021-06-22 stsp static const struct got_error *
864 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
866 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
867 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
869 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
870 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
871 05118f5a 2021-06-22 stsp char *id_str = NULL;
872 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
873 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
874 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
876 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
877 05118f5a 2021-06-22 stsp switch (ch) {
879 05118f5a 2021-06-22 stsp usage_indexpack();
880 05118f5a 2021-06-22 stsp /* NOTREACHED */
884 05118f5a 2021-06-22 stsp argc -= optind;
885 05118f5a 2021-06-22 stsp argv += optind;
887 05118f5a 2021-06-22 stsp if (argc != 1)
888 05118f5a 2021-06-22 stsp usage_indexpack();
890 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
891 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
892 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
894 05118f5a 2021-06-22 stsp #ifndef PROFILE
895 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
896 05118f5a 2021-06-22 stsp NULL) == -1)
897 05118f5a 2021-06-22 stsp err(1, "pledge");
900 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
901 7cd52833 2022-06-23 thomas if (error != NULL)
902 7cd52833 2022-06-23 thomas goto done;
903 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
907 001a95cd 2021-10-15 thomas error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
911 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
912 fdbea373 2023-07-10 thomas ppa.out = stdout;
913 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
914 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
915 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
917 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
918 05118f5a 2021-06-22 stsp packfile_path);
922 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
926 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
927 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
930 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
932 c75a6067 2021-10-15 thomas if (repo)
933 c75a6067 2021-10-15 thomas got_repo_close(repo);
934 7cd52833 2022-06-23 thomas if (pack_fds) {
935 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
936 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
937 7cd52833 2022-06-23 thomas if (error == NULL)
938 7cd52833 2022-06-23 thomas error = pack_err;
940 05118f5a 2021-06-22 stsp free(id_str);
941 05118f5a 2021-06-22 stsp free(pack_hash);
942 05118f5a 2021-06-22 stsp return error;
945 05118f5a 2021-06-22 stsp __dead static void
946 05118f5a 2021-06-22 stsp usage_listpack(void)
948 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
949 05118f5a 2021-06-22 stsp getprogname());
953 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
954 05118f5a 2021-06-22 stsp int nblobs;
955 05118f5a 2021-06-22 stsp int ntrees;
956 05118f5a 2021-06-22 stsp int ncommits;
958 05118f5a 2021-06-22 stsp int noffdeltas;
959 05118f5a 2021-06-22 stsp int nrefdeltas;
960 05118f5a 2021-06-22 stsp int human_readable;
963 05118f5a 2021-06-22 stsp static const struct got_error *
964 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
965 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
967 05118f5a 2021-06-22 stsp const struct got_error *err;
968 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
969 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
970 05118f5a 2021-06-22 stsp const char *type_str;
972 7cd52833 2022-06-23 thomas err = got_object_id_str(&id_str, id);
974 05118f5a 2021-06-22 stsp return err;
976 05118f5a 2021-06-22 stsp switch (type) {
977 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
978 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
979 05118f5a 2021-06-22 stsp a->nblobs++;
981 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
982 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
983 05118f5a 2021-06-22 stsp a->ntrees++;
985 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
986 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
987 05118f5a 2021-06-22 stsp a->ncommits++;
989 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
990 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
991 05118f5a 2021-06-22 stsp a->ntags++;
993 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
994 05118f5a 2021-06-22 stsp type_str = "offset-delta";
995 c414a013 2021-09-25 thomas.ad if (asprintf(&delta_str, " base-offset %lld",
996 c414a013 2021-09-25 thomas.ad (long long)base_offset) == -1) {
997 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
1000 05118f5a 2021-06-22 stsp a->noffdeltas++;
1002 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
1003 05118f5a 2021-06-22 stsp type_str = "ref-delta";
1004 b6b86fd1 2022-08-30 thomas err = got_object_id_str(&base_id_str, base_id);
1006 05118f5a 2021-06-22 stsp goto done;
1007 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1008 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
1009 05118f5a 2021-06-22 stsp goto done;
1011 05118f5a 2021-06-22 stsp a->nrefdeltas++;
1014 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1015 05118f5a 2021-06-22 stsp goto done;
1017 05118f5a 2021-06-22 stsp if (a->human_readable) {
1018 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
1020 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
1021 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
1022 05118f5a 2021-06-22 stsp goto done;
1024 05118f5a 2021-06-22 stsp s = scaled;
1025 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
1027 c414a013 2021-09-25 thomas.ad printf("%s %s at %lld size %s%s\n", id_str, type_str,
1028 c414a013 2021-09-25 thomas.ad (long long)offset, s, delta_str ? delta_str : "");
1030 c414a013 2021-09-25 thomas.ad printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1031 c414a013 2021-09-25 thomas.ad (long long)offset, (long long)size,
1032 c414a013 2021-09-25 thomas.ad delta_str ? delta_str : "");
1035 05118f5a 2021-06-22 stsp free(id_str);
1036 05118f5a 2021-06-22 stsp free(base_id_str);
1037 05118f5a 2021-06-22 stsp free(delta_str);
1038 05118f5a 2021-06-22 stsp return err;
1041 05118f5a 2021-06-22 stsp static const struct got_error *
1042 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
1044 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
1045 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
1047 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
1048 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
1049 05118f5a 2021-06-22 stsp char *id_str = NULL;
1050 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
1051 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
1052 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
1053 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
1055 a0abeae5 2023-02-17 thomas #ifndef PROFILE
1056 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1057 a0abeae5 2023-02-17 thomas NULL) == -1)
1058 a0abeae5 2023-02-17 thomas err(1, "pledge");
1061 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
1062 05118f5a 2021-06-22 stsp switch (ch) {
1064 05118f5a 2021-06-22 stsp human_readable = 1;
1067 05118f5a 2021-06-22 stsp show_stats = 1;
1070 05118f5a 2021-06-22 stsp usage_listpack();
1071 05118f5a 2021-06-22 stsp /* NOTREACHED */
1075 05118f5a 2021-06-22 stsp argc -= optind;
1076 05118f5a 2021-06-22 stsp argv += optind;
1078 05118f5a 2021-06-22 stsp if (argc != 1)
1079 05118f5a 2021-06-22 stsp usage_listpack();
1080 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
1081 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
1082 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
1084 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1085 7cd52833 2022-06-23 thomas if (error != NULL)
1086 7cd52833 2022-06-23 thomas goto done;
1087 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1088 05118f5a 2021-06-22 stsp if (error)
1089 05118f5a 2021-06-22 stsp goto done;
1090 bfb5ee0b 2022-05-31 thomas #ifndef PROFILE
1091 bfb5ee0b 2022-05-31 thomas /* Remove "cpath" promise. */
1092 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1093 bfb5ee0b 2022-05-31 thomas NULL) == -1)
1094 bfb5ee0b 2022-05-31 thomas err(1, "pledge");
1096 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1097 05118f5a 2021-06-22 stsp if (error)
1098 05118f5a 2021-06-22 stsp goto done;
1100 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
1101 05118f5a 2021-06-22 stsp packfile_path);
1102 05118f5a 2021-06-22 stsp if (error)
1103 05118f5a 2021-06-22 stsp goto done;
1104 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
1105 05118f5a 2021-06-22 stsp if (error)
1106 05118f5a 2021-06-22 stsp goto done;
1108 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
1109 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
1110 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
1111 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
1112 05118f5a 2021-06-22 stsp if (error)
1113 05118f5a 2021-06-22 stsp goto done;
1114 05118f5a 2021-06-22 stsp if (show_stats) {
1115 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1116 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1117 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1118 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
1119 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1120 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
1123 c75a6067 2021-10-15 thomas if (repo)
1124 c75a6067 2021-10-15 thomas got_repo_close(repo);
1125 7cd52833 2022-06-23 thomas if (pack_fds) {
1126 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
1127 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
1128 7cd52833 2022-06-23 thomas if (error == NULL)
1129 7cd52833 2022-06-23 thomas error = pack_err;
1131 05118f5a 2021-06-22 stsp free(id_str);
1132 05118f5a 2021-06-22 stsp free(pack_hash);
1133 05118f5a 2021-06-22 stsp free(packfile_path);
1134 05118f5a 2021-06-22 stsp return error;
1137 b3d68e7f 2021-07-03 stsp __dead static void
1138 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1140 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1141 d6506a3d 2022-08-16 thomas getprogname());
1145 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1146 b3d68e7f 2021-07-03 stsp int last_nloose;
1147 b3d68e7f 2021-07-03 stsp int last_ncommits;
1148 b3d68e7f 2021-07-03 stsp int last_npurged;
1149 afd0da38 2023-06-22 thomas int last_nredundant;
1150 b3d68e7f 2021-07-03 stsp int verbosity;
1151 b3d68e7f 2021-07-03 stsp int printed_something;
1152 b3d68e7f 2021-07-03 stsp int dry_run;
1155 b3d68e7f 2021-07-03 stsp static const struct got_error *
1156 6322ae43 2023-07-11 thomas cleanup_progress(void *arg, int ncommits, int nloose, int npurged,
1157 afd0da38 2023-06-22 thomas int nredundant)
1159 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1160 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1161 afd0da38 2023-06-22 thomas int print_redundant = 0;
1163 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1164 b3d68e7f 2021-07-03 stsp print_commits = 1;
1165 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1167 6322ae43 2023-07-11 thomas if (a->last_nloose != nloose) {
1168 6322ae43 2023-07-11 thomas print_commits = 1;
1169 b3d68e7f 2021-07-03 stsp print_loose = 1;
1170 6322ae43 2023-07-11 thomas a->last_nloose = nloose;
1172 6322ae43 2023-07-11 thomas if (a->last_npurged != npurged) {
1173 b3d68e7f 2021-07-03 stsp print_commits = 1;
1174 6322ae43 2023-07-11 thomas print_loose = 1;
1175 b3d68e7f 2021-07-03 stsp print_purged = 1;
1176 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1178 afd0da38 2023-06-22 thomas if (a->last_nredundant != nredundant) {
1179 91554d23 2023-06-25 thomas print_commits = 1;
1180 6322ae43 2023-07-11 thomas print_loose = 1;
1181 91554d23 2023-06-25 thomas print_purged = 1;
1182 afd0da38 2023-06-22 thomas print_redundant = 1;
1183 afd0da38 2023-06-22 thomas a->last_nredundant = nredundant;
1186 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1187 b3d68e7f 2021-07-03 stsp return NULL;
1189 afd0da38 2023-06-22 thomas if (print_loose || print_commits || print_purged || print_redundant)
1190 b3d68e7f 2021-07-03 stsp printf("\r");
1191 b3d68e7f 2021-07-03 stsp if (print_commits)
1192 6322ae43 2023-07-11 thomas printf("%d commit%s scanned", ncommits,
1193 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1194 6322ae43 2023-07-11 thomas if (print_loose)
1195 6322ae43 2023-07-11 thomas printf("; %d loose object%s", nloose, nloose == 1 ? "" : "s");
1196 91554d23 2023-06-25 thomas if (print_purged || print_redundant) {
1197 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1198 91554d23 2023-06-25 thomas printf("; could purge %d object%s", npurged,
1199 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1201 91554d23 2023-06-25 thomas printf("; purged %d object%s", npurged,
1202 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1205 afd0da38 2023-06-22 thomas if (print_redundant) {
1206 afd0da38 2023-06-22 thomas if (a->dry_run) {
1207 91554d23 2023-06-25 thomas printf(", %d pack file%s", nredundant,
1208 afd0da38 2023-06-22 thomas nredundant == 1 ? "" : "s");
1209 afd0da38 2023-06-22 thomas } else {
1210 91554d23 2023-06-25 thomas printf(", %d pack file%s", nredundant,
1211 afd0da38 2023-06-22 thomas nredundant == 1 ? "" : "s");
1214 afd0da38 2023-06-22 thomas if (print_loose || print_commits || print_purged || print_redundant) {
1215 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1216 b3d68e7f 2021-07-03 stsp fflush(stdout);
1218 b3d68e7f 2021-07-03 stsp return NULL;
1221 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1222 1124fe40 2021-07-07 stsp int verbosity;
1223 1124fe40 2021-07-07 stsp int printed_something;
1224 1124fe40 2021-07-07 stsp int dry_run;
1227 b3d68e7f 2021-07-03 stsp static const struct got_error *
1228 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1230 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1232 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1233 1124fe40 2021-07-07 stsp return NULL;
1235 1124fe40 2021-07-07 stsp if (a->dry_run)
1236 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1238 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1240 1124fe40 2021-07-07 stsp a->printed_something = 1;
1241 1124fe40 2021-07-07 stsp return NULL;
1244 1124fe40 2021-07-07 stsp static const struct got_error *
1245 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1247 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1248 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
1249 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1250 91554d23 2023-06-25 thomas int ch, dry_run = 0, verbosity = 0;
1251 91554d23 2023-06-25 thomas int ncommits = 0, nloose = 0, npacked = 0;
1252 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1253 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1254 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1255 afd0da38 2023-06-22 thomas off_t loose_before, loose_after;
1256 afd0da38 2023-06-22 thomas off_t pack_before, pack_after;
1257 afd0da38 2023-06-22 thomas off_t total_size;
1258 afd0da38 2023-06-22 thomas char loose_before_scaled[FMT_SCALED_STRSIZE];
1259 afd0da38 2023-06-22 thomas char loose_after_scaled[FMT_SCALED_STRSIZE];
1260 afd0da38 2023-06-22 thomas char pack_before_scaled[FMT_SCALED_STRSIZE];
1261 afd0da38 2023-06-22 thomas char pack_after_scaled[FMT_SCALED_STRSIZE];
1262 afd0da38 2023-06-22 thomas char total_size_scaled[FMT_SCALED_STRSIZE];
1263 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
1265 a0abeae5 2023-02-17 thomas #ifndef PROFILE
1266 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1267 a0abeae5 2023-02-17 thomas NULL) == -1)
1268 a0abeae5 2023-02-17 thomas err(1, "pledge");
1271 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1272 b3d68e7f 2021-07-03 stsp switch (ch) {
1274 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1276 f7065961 2022-10-27 thomas case 'n':
1277 f7065961 2022-10-27 thomas dry_run = 1;
1280 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1282 f7065961 2022-10-27 thomas case 'q':
1283 f7065961 2022-10-27 thomas verbosity = -1;
1286 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1287 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1288 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1290 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1293 b3d68e7f 2021-07-03 stsp usage_cleanup();
1294 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1298 b3d68e7f 2021-07-03 stsp argc -= optind;
1299 b3d68e7f 2021-07-03 stsp argv += optind;
1301 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
1302 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
1303 1ea7ccc6 2021-11-15 thomas if (error)
1304 1ea7ccc6 2021-11-15 thomas goto done;
1306 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1307 7cd52833 2022-06-23 thomas if (error != NULL)
1308 7cd52833 2022-06-23 thomas goto done;
1309 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1310 b3d68e7f 2021-07-03 stsp if (error)
1311 b3d68e7f 2021-07-03 stsp goto done;
1313 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1314 b3d68e7f 2021-07-03 stsp if (error)
1315 b3d68e7f 2021-07-03 stsp goto done;
1317 2624d165 2023-02-07 thomas if (got_repo_has_extension(repo, "preciousObjects")) {
1318 2624d165 2023-02-07 thomas error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1319 2624d165 2023-02-07 thomas "the preciousObjects Git extension is enabled; "
1320 2624d165 2023-02-07 thomas "this implies that objects must not be deleted");
1321 2624d165 2023-02-07 thomas goto done;
1324 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1325 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1326 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1327 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1328 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1329 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1330 1124fe40 2021-07-07 stsp goto done;
1333 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1334 6322ae43 2023-07-11 thomas cpa.last_nloose = -1;
1335 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1336 afd0da38 2023-06-22 thomas cpa.last_nredundant = -1;
1337 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1338 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1340 6322ae43 2023-07-11 thomas error = got_repo_cleanup(repo, &loose_before, &loose_after,
1341 6322ae43 2023-07-11 thomas &pack_before, &pack_after, &ncommits, &nloose, &npacked,
1342 91554d23 2023-06-25 thomas dry_run, ignore_mtime, cleanup_progress, &cpa,
1343 91554d23 2023-06-25 thomas check_cancelled, NULL);
1344 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1345 b3d68e7f 2021-07-03 stsp printf("\n");
1346 afd0da38 2023-06-22 thomas if (error)
1347 afd0da38 2023-06-22 thomas goto done;
1349 afd0da38 2023-06-22 thomas total_size = (loose_before - loose_after) + (pack_before - pack_after);
1351 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1352 afd0da38 2023-06-22 thomas if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1353 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1354 b3d68e7f 2021-07-03 stsp goto done;
1356 afd0da38 2023-06-22 thomas if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1357 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1358 b3d68e7f 2021-07-03 stsp goto done;
1360 afd0da38 2023-06-22 thomas if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1361 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1362 b3d68e7f 2021-07-03 stsp goto done;
1364 afd0da38 2023-06-22 thomas if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1365 afd0da38 2023-06-22 thomas error = got_error_from_errno("fmt_scaled");
1366 afd0da38 2023-06-22 thomas goto done;
1368 afd0da38 2023-06-22 thomas if (fmt_scaled(total_size, total_size_scaled) == -1) {
1369 afd0da38 2023-06-22 thomas error = got_error_from_errno("fmt_scaled");
1370 afd0da38 2023-06-22 thomas goto done;
1372 afd0da38 2023-06-22 thomas printf("loose total size before: %s\n", loose_before_scaled);
1373 afd0da38 2023-06-22 thomas printf("loose total size after: %s\n", loose_after_scaled);
1374 afd0da38 2023-06-22 thomas printf("pack files total size before: %s\n",
1375 afd0da38 2023-06-22 thomas pack_before_scaled);
1376 afd0da38 2023-06-22 thomas printf("pack files total size after: %s\n", pack_after_scaled);
1377 b3d68e7f 2021-07-03 stsp if (dry_run) {
1378 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1379 afd0da38 2023-06-22 thomas total_size_scaled);
1381 afd0da38 2023-06-22 thomas printf("disk space freed: %s\n", total_size_scaled);
1382 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1387 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1388 7cd52833 2022-06-23 thomas if (pack_fds) {
1389 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
1390 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
1391 fdbea373 2023-07-10 thomas if (error == NULL)
1392 fdbea373 2023-07-10 thomas error = pack_err;
1394 fdbea373 2023-07-10 thomas free(repo_path);
1395 fdbea373 2023-07-10 thomas return error;
1398 fdbea373 2023-07-10 thomas __dead static void
1399 fdbea373 2023-07-10 thomas usage_dump(void)
1401 fdbea373 2023-07-10 thomas fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1402 fdbea373 2023-07-10 thomas "[-x reference] [reference]...\n", getprogname());
1403 fdbea373 2023-07-10 thomas exit(1);
1406 fdbea373 2023-07-10 thomas static const struct got_error *
1407 fdbea373 2023-07-10 thomas cmd_dump(int argc, char *argv[])
1409 fdbea373 2023-07-10 thomas const struct got_error *error = NULL;
1410 fdbea373 2023-07-10 thomas struct got_pack_progress_arg ppa;
1411 fdbea373 2023-07-10 thomas struct got_repository *repo = NULL;
1412 fdbea373 2023-07-10 thomas struct got_pathlist_head exclude_args;
1413 fdbea373 2023-07-10 thomas struct got_pathlist_entry *pe;
1414 fdbea373 2023-07-10 thomas struct got_reflist_head exclude_refs;
1415 fdbea373 2023-07-10 thomas struct got_reflist_head include_refs;
1416 fdbea373 2023-07-10 thomas struct got_reflist_entry *re, *new;
1417 fdbea373 2023-07-10 thomas const char *refname;
1418 fdbea373 2023-07-10 thomas char *repo_path = NULL;
1419 fdbea373 2023-07-10 thomas int *pack_fds = NULL;
1420 fdbea373 2023-07-10 thomas int verbosity = 0;
1421 fdbea373 2023-07-10 thomas int i, ch;
1423 fdbea373 2023-07-10 thomas TAILQ_INIT(&exclude_args);
1424 fdbea373 2023-07-10 thomas TAILQ_INIT(&exclude_refs);
1425 fdbea373 2023-07-10 thomas TAILQ_INIT(&include_refs);
1427 fdbea373 2023-07-10 thomas #ifndef PROFILE
1428 fdbea373 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1429 fdbea373 2023-07-10 thomas NULL) == -1)
1430 fdbea373 2023-07-10 thomas err(1, "pledge");
1433 fdbea373 2023-07-10 thomas while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1434 fdbea373 2023-07-10 thomas switch (ch) {
1435 fdbea373 2023-07-10 thomas case 'q':
1436 fdbea373 2023-07-10 thomas verbosity = -1;
1438 fdbea373 2023-07-10 thomas case 'r':
1439 fdbea373 2023-07-10 thomas repo_path = realpath(optarg, NULL);
1440 fdbea373 2023-07-10 thomas if (repo_path == NULL)
1441 fdbea373 2023-07-10 thomas return got_error_from_errno2("realpath",
1442 fdbea373 2023-07-10 thomas optarg);
1443 fdbea373 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
1445 fdbea373 2023-07-10 thomas case 'x':
1446 fdbea373 2023-07-10 thomas error = got_pathlist_append(&exclude_args,
1447 fdbea373 2023-07-10 thomas optarg, NULL);
1448 fdbea373 2023-07-10 thomas if (error)
1449 fdbea373 2023-07-10 thomas return error;
1451 fdbea373 2023-07-10 thomas default:
1452 fdbea373 2023-07-10 thomas usage_dump();
1453 fdbea373 2023-07-10 thomas /* NOTREACHED */
1456 fdbea373 2023-07-10 thomas argc -= optind;
1457 fdbea373 2023-07-10 thomas argv += optind;
1459 fdbea373 2023-07-10 thomas if (repo_path == NULL) {
1460 fdbea373 2023-07-10 thomas error = get_repo_path(&repo_path);
1461 fdbea373 2023-07-10 thomas if (error)
1462 fdbea373 2023-07-10 thomas goto done;
1464 fdbea373 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1465 fdbea373 2023-07-10 thomas if (error != NULL)
1466 fdbea373 2023-07-10 thomas goto done;
1467 fdbea373 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1468 fdbea373 2023-07-10 thomas if (error)
1469 fdbea373 2023-07-10 thomas goto done;
1471 fdbea373 2023-07-10 thomas error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1472 fdbea373 2023-07-10 thomas if (error)
1473 fdbea373 2023-07-10 thomas goto done;
1475 fdbea373 2023-07-10 thomas TAILQ_FOREACH(pe, &exclude_args, entry) {
1476 fdbea373 2023-07-10 thomas refname = pe->path;
1477 fdbea373 2023-07-10 thomas error = add_ref(&new, &exclude_refs, refname, repo);
1478 fdbea373 2023-07-10 thomas if (error)
1479 fdbea373 2023-07-10 thomas goto done;
1482 fdbea373 2023-07-10 thomas if (argc == 0) {
1483 fdbea373 2023-07-10 thomas error = got_ref_list(&include_refs, repo, "",
1484 fdbea373 2023-07-10 thomas got_ref_cmp_by_name, NULL);
1485 fdbea373 2023-07-10 thomas if (error)
1486 fdbea373 2023-07-10 thomas goto done;
1487 fdbea373 2023-07-10 thomas } else {
1488 fdbea373 2023-07-10 thomas for (i = 0; i < argc; i++) {
1489 fdbea373 2023-07-10 thomas got_path_strip_trailing_slashes(argv[i]);
1490 fdbea373 2023-07-10 thomas refname = argv[i];
1491 fdbea373 2023-07-10 thomas error = add_ref(&new, &include_refs, refname, repo);
1492 fdbea373 2023-07-10 thomas if (error)
1493 fdbea373 2023-07-10 thomas goto done;
1497 fdbea373 2023-07-10 thomas /* Ignore references in the refs/got/ namespace. */
1498 fdbea373 2023-07-10 thomas TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1499 fdbea373 2023-07-10 thomas refname = got_ref_get_name(re->ref);
1500 fdbea373 2023-07-10 thomas if (strncmp("refs/got/", refname, 9) != 0)
1501 fdbea373 2023-07-10 thomas continue;
1502 fdbea373 2023-07-10 thomas TAILQ_REMOVE(&include_refs, re, entry);
1503 fdbea373 2023-07-10 thomas got_ref_close(re->ref);
1504 fdbea373 2023-07-10 thomas free(re);
1507 fdbea373 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
1508 fdbea373 2023-07-10 thomas ppa.out = stderr;
1509 fdbea373 2023-07-10 thomas ppa.verbosity = verbosity;
1511 fdbea373 2023-07-10 thomas error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1512 fdbea373 2023-07-10 thomas repo, pack_progress, &ppa, check_cancelled, NULL);
1513 fdbea373 2023-07-10 thomas if (ppa.printed_something)
1514 fdbea373 2023-07-10 thomas fprintf(stderr, "\n");
1516 fdbea373 2023-07-10 thomas if (repo)
1517 fdbea373 2023-07-10 thomas got_repo_close(repo);
1519 fdbea373 2023-07-10 thomas if (pack_fds) {
1520 fdbea373 2023-07-10 thomas const struct got_error *pack_err;
1522 fdbea373 2023-07-10 thomas pack_err = got_repo_pack_fds_close(pack_fds);
1523 7cd52833 2022-06-23 thomas if (error == NULL)
1524 7cd52833 2022-06-23 thomas error = pack_err;
1527 fdbea373 2023-07-10 thomas got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1528 fdbea373 2023-07-10 thomas got_ref_list_free(&exclude_refs);
1529 fdbea373 2023-07-10 thomas got_ref_list_free(&include_refs);
1530 90afc9f3 2023-07-10 thomas free(repo_path);
1532 90afc9f3 2023-07-10 thomas return error;
1535 90afc9f3 2023-07-10 thomas __dead static void
1536 90afc9f3 2023-07-10 thomas usage_load(void)
1538 cfbfa60c 2023-07-11 thomas fprintf(stderr, "usage: %s load [-nq] [-l bundle-file] "
1539 cfbfa60c 2023-07-11 thomas "[-r repository-path] [reference ...]\n",
1540 90afc9f3 2023-07-10 thomas getprogname());
1541 90afc9f3 2023-07-10 thomas exit(1);
1544 90afc9f3 2023-07-10 thomas static const struct got_error *
1545 90afc9f3 2023-07-10 thomas load_progress(void *arg, off_t packfile_size, int nobj_total,
1546 90afc9f3 2023-07-10 thomas int nobj_indexed, int nobj_loose, int nobj_resolved)
1548 90afc9f3 2023-07-10 thomas return pack_index_progress(arg, packfile_size, nobj_total,
1549 90afc9f3 2023-07-10 thomas nobj_indexed, nobj_loose, nobj_resolved);
1552 90afc9f3 2023-07-10 thomas static int
1553 90afc9f3 2023-07-10 thomas is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1555 90afc9f3 2023-07-10 thomas struct got_pathlist_entry *pe;
1557 90afc9f3 2023-07-10 thomas if (TAILQ_EMPTY(wanted))
1558 90afc9f3 2023-07-10 thomas return 1;
1560 90afc9f3 2023-07-10 thomas TAILQ_FOREACH(pe, wanted, entry) {
1561 90afc9f3 2023-07-10 thomas if (strcmp(pe->path, ref) == 0)
1562 90afc9f3 2023-07-10 thomas return 1;
1565 90afc9f3 2023-07-10 thomas return 0;
1568 90afc9f3 2023-07-10 thomas static const struct got_error *
1569 90afc9f3 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
1570 90afc9f3 2023-07-10 thomas int verbosity, struct got_repository *repo)
1572 90afc9f3 2023-07-10 thomas const struct got_error *err = NULL;
1573 90afc9f3 2023-07-10 thomas struct got_reference *ref;
1574 90afc9f3 2023-07-10 thomas char *id_str;
1576 90afc9f3 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1577 90afc9f3 2023-07-10 thomas if (err)
1578 90afc9f3 2023-07-10 thomas return err;
1580 90afc9f3 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
1581 90afc9f3 2023-07-10 thomas if (err)
1582 90afc9f3 2023-07-10 thomas goto done;
1584 90afc9f3 2023-07-10 thomas err = got_ref_write(ref, repo);
1585 90afc9f3 2023-07-10 thomas got_ref_close(ref);
1587 90afc9f3 2023-07-10 thomas if (err == NULL && verbosity >= 0)
1588 90afc9f3 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
1590 90afc9f3 2023-07-10 thomas free(id_str);
1591 90afc9f3 2023-07-10 thomas return err;
1594 90afc9f3 2023-07-10 thomas static const struct got_error *
1595 90afc9f3 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
1596 90afc9f3 2023-07-10 thomas int replace_tags, int verbosity, struct got_repository *repo)
1598 90afc9f3 2023-07-10 thomas const struct got_error *err = NULL;
1599 90afc9f3 2023-07-10 thomas char *new_id_str = NULL;
1600 90afc9f3 2023-07-10 thomas struct got_object_id *old_id = NULL;
1602 90afc9f3 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
1603 90afc9f3 2023-07-10 thomas if (err)
1604 90afc9f3 2023-07-10 thomas goto done;
1606 90afc9f3 2023-07-10 thomas if (!replace_tags &&
1607 90afc9f3 2023-07-10 thomas strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1608 90afc9f3 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1609 90afc9f3 2023-07-10 thomas if (err)
1610 90afc9f3 2023-07-10 thomas goto done;
1611 90afc9f3 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1612 90afc9f3 2023-07-10 thomas goto done;
1613 90afc9f3 2023-07-10 thomas if (verbosity >= 0) {
1614 90afc9f3 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
1615 90afc9f3 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
1617 90afc9f3 2023-07-10 thomas goto done;
1620 90afc9f3 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
1621 90afc9f3 2023-07-10 thomas if (verbosity >= 0) {
1622 90afc9f3 2023-07-10 thomas printf("Replacing reference %s: %s\n",
1623 90afc9f3 2023-07-10 thomas got_ref_get_name(ref),
1624 90afc9f3 2023-07-10 thomas got_ref_get_symref_target(ref));
1626 90afc9f3 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
1627 90afc9f3 2023-07-10 thomas if (err)
1628 90afc9f3 2023-07-10 thomas goto done;
1629 90afc9f3 2023-07-10 thomas err = got_ref_write(ref, repo);
1630 90afc9f3 2023-07-10 thomas if (err)
1631 90afc9f3 2023-07-10 thomas goto done;
1632 90afc9f3 2023-07-10 thomas } else {
1633 90afc9f3 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1634 90afc9f3 2023-07-10 thomas if (err)
1635 90afc9f3 2023-07-10 thomas goto done;
1636 90afc9f3 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1637 90afc9f3 2023-07-10 thomas goto done;
1639 90afc9f3 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
1640 90afc9f3 2023-07-10 thomas if (err)
1641 90afc9f3 2023-07-10 thomas goto done;
1642 90afc9f3 2023-07-10 thomas err = got_ref_write(ref, repo);
1643 90afc9f3 2023-07-10 thomas if (err)
1644 90afc9f3 2023-07-10 thomas goto done;
1647 90afc9f3 2023-07-10 thomas if (verbosity >= 0)
1648 90afc9f3 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
1649 90afc9f3 2023-07-10 thomas new_id_str);
1651 90afc9f3 2023-07-10 thomas free(old_id);
1652 90afc9f3 2023-07-10 thomas free(new_id_str);
1653 90afc9f3 2023-07-10 thomas return err;
1656 90afc9f3 2023-07-10 thomas static const struct got_error *
1657 90afc9f3 2023-07-10 thomas cmd_load(int argc, char *argv[])
1659 90afc9f3 2023-07-10 thomas const struct got_error *error = NULL;
1660 90afc9f3 2023-07-10 thomas struct got_repository *repo = NULL;
1661 90afc9f3 2023-07-10 thomas struct got_pathlist_head include_args;
1662 90afc9f3 2023-07-10 thomas struct got_pathlist_head available_refs;
1663 90afc9f3 2023-07-10 thomas struct got_pathlist_entry *pe;
1664 90afc9f3 2023-07-10 thomas struct got_pack_progress_arg ppa;
1665 90afc9f3 2023-07-10 thomas FILE *in = stdin;
1666 90afc9f3 2023-07-10 thomas int *pack_fds = NULL;
1667 90afc9f3 2023-07-10 thomas char *repo_path = NULL;
1668 90afc9f3 2023-07-10 thomas int list_refs_only = 0;
1669 90afc9f3 2023-07-10 thomas int noop = 0;
1670 90afc9f3 2023-07-10 thomas int verbosity = 0;
1671 cfbfa60c 2023-07-11 thomas int ch, i;
1673 90afc9f3 2023-07-10 thomas TAILQ_INIT(&include_args);
1674 90afc9f3 2023-07-10 thomas TAILQ_INIT(&available_refs);
1676 90afc9f3 2023-07-10 thomas #ifndef PROFILE
1677 90afc9f3 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1678 90afc9f3 2023-07-10 thomas "sendfd unveil", NULL) == -1)
1679 90afc9f3 2023-07-10 thomas err(1, "pledge");
1682 cfbfa60c 2023-07-11 thomas while ((ch = getopt(argc, argv, "l:nqr:")) != -1) {
1683 90afc9f3 2023-07-10 thomas switch (ch) {
1684 90afc9f3 2023-07-10 thomas case 'l':
1685 90afc9f3 2023-07-10 thomas list_refs_only = 1;
1686 cfbfa60c 2023-07-11 thomas in = fopen(optarg, "re");
1687 cfbfa60c 2023-07-11 thomas if (in == NULL)
1688 cfbfa60c 2023-07-11 thomas return got_error_from_errno2("open", optarg);
1690 90afc9f3 2023-07-10 thomas case 'n':
1691 90afc9f3 2023-07-10 thomas noop = 1;
1693 90afc9f3 2023-07-10 thomas case 'q':
1694 90afc9f3 2023-07-10 thomas verbosity = -1;
1696 90afc9f3 2023-07-10 thomas case 'r':
1697 90afc9f3 2023-07-10 thomas repo_path = realpath(optarg, NULL);
1698 90afc9f3 2023-07-10 thomas if (repo_path == NULL)
1699 90afc9f3 2023-07-10 thomas return got_error_from_errno2("realpath",
1700 90afc9f3 2023-07-10 thomas optarg);
1701 90afc9f3 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
1703 90afc9f3 2023-07-10 thomas default:
1704 90afc9f3 2023-07-10 thomas usage_load();
1705 90afc9f3 2023-07-10 thomas /* NOTREACHED */
1708 90afc9f3 2023-07-10 thomas argc -= optind;
1709 90afc9f3 2023-07-10 thomas argv += optind;
1711 cfbfa60c 2023-07-11 thomas if (list_refs_only && argc > 1)
1712 cfbfa60c 2023-07-11 thomas errx(1, "-l and references on the command line are exclusive");
1713 90afc9f3 2023-07-10 thomas if (list_refs_only && noop)
1714 90afc9f3 2023-07-10 thomas errx(1, "-n and -l are mutually exclusive");
1716 cfbfa60c 2023-07-11 thomas for (i = 0; i < argc; i++) {
1717 cfbfa60c 2023-07-11 thomas char *refname = argv[i];
1718 cfbfa60c 2023-07-11 thomas got_path_strip_trailing_slashes(refname);
1719 cfbfa60c 2023-07-11 thomas if (!got_ref_name_is_valid(refname))
1720 cfbfa60c 2023-07-11 thomas errx(1, "invalid reference name %s", refname);
1721 cfbfa60c 2023-07-11 thomas error = got_pathlist_append(&include_args, refname, NULL);
1722 cfbfa60c 2023-07-11 thomas if (error)
1723 cfbfa60c 2023-07-11 thomas goto done;
1726 90afc9f3 2023-07-10 thomas if (repo_path == NULL) {
1727 90afc9f3 2023-07-10 thomas error = get_repo_path(&repo_path);
1728 90afc9f3 2023-07-10 thomas if (error)
1729 90afc9f3 2023-07-10 thomas goto done;
1731 90afc9f3 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1732 90afc9f3 2023-07-10 thomas if (error != NULL)
1733 90afc9f3 2023-07-10 thomas goto done;
1734 90afc9f3 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1735 90afc9f3 2023-07-10 thomas if (error)
1736 90afc9f3 2023-07-10 thomas goto done;
1738 90afc9f3 2023-07-10 thomas error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1739 90afc9f3 2023-07-10 thomas if (error)
1740 90afc9f3 2023-07-10 thomas goto done;
1742 90afc9f3 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
1743 90afc9f3 2023-07-10 thomas ppa.out = stdout;
1744 90afc9f3 2023-07-10 thomas ppa.verbosity = verbosity;
1746 90afc9f3 2023-07-10 thomas error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1747 90afc9f3 2023-07-10 thomas load_progress, &ppa, check_cancelled, NULL);
1748 d9595352 2023-07-10 thomas if (verbosity >= 0 && !list_refs_only)
1749 90afc9f3 2023-07-10 thomas printf("\n");
1750 90afc9f3 2023-07-10 thomas if (error)
1751 90afc9f3 2023-07-10 thomas goto done;
1753 90afc9f3 2023-07-10 thomas if (list_refs_only) {
1754 90afc9f3 2023-07-10 thomas TAILQ_FOREACH(pe, &available_refs, entry) {
1755 90afc9f3 2023-07-10 thomas const char *refname = pe->path;
1756 90afc9f3 2023-07-10 thomas struct got_object_id *id = pe->data;
1757 90afc9f3 2023-07-10 thomas char *idstr;
1759 90afc9f3 2023-07-10 thomas error = got_object_id_str(&idstr, id);
1760 90afc9f3 2023-07-10 thomas if (error)
1761 90afc9f3 2023-07-10 thomas goto done;
1763 90afc9f3 2023-07-10 thomas printf("%s: %s\n", refname, idstr);
1764 90afc9f3 2023-07-10 thomas free(idstr);
1766 90afc9f3 2023-07-10 thomas goto done;
1769 90afc9f3 2023-07-10 thomas if (noop)
1770 90afc9f3 2023-07-10 thomas goto done;
1772 90afc9f3 2023-07-10 thomas /* Update references */
1773 90afc9f3 2023-07-10 thomas TAILQ_FOREACH(pe, &available_refs, entry) {
1774 90afc9f3 2023-07-10 thomas const struct got_error *unlock_err;
1775 90afc9f3 2023-07-10 thomas struct got_reference *ref;
1776 90afc9f3 2023-07-10 thomas const char *refname = pe->path;
1777 90afc9f3 2023-07-10 thomas struct got_object_id *id = pe->data;
1779 90afc9f3 2023-07-10 thomas if (!is_wanted_ref(&include_args, pe->path))
1780 90afc9f3 2023-07-10 thomas continue;
1782 90afc9f3 2023-07-10 thomas error = got_ref_open(&ref, repo, refname, 1);
1783 90afc9f3 2023-07-10 thomas if (error) {
1784 90afc9f3 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
1785 90afc9f3 2023-07-10 thomas goto done;
1786 90afc9f3 2023-07-10 thomas error = create_ref(refname, id, verbosity, repo);
1787 90afc9f3 2023-07-10 thomas if (error)
1788 90afc9f3 2023-07-10 thomas goto done;
1789 90afc9f3 2023-07-10 thomas } else {
1790 90afc9f3 2023-07-10 thomas /* XXX: check advances only and add -f to force? */
1791 90afc9f3 2023-07-10 thomas error = update_ref(ref, id, 1, verbosity, repo);
1792 90afc9f3 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
1793 90afc9f3 2023-07-10 thomas if (unlock_err && error == NULL)
1794 90afc9f3 2023-07-10 thomas error = unlock_err;
1795 90afc9f3 2023-07-10 thomas got_ref_close(ref);
1796 90afc9f3 2023-07-10 thomas if (error)
1797 90afc9f3 2023-07-10 thomas goto done;
1802 90afc9f3 2023-07-10 thomas if (in != stdin && fclose(in) == EOF && error == NULL)
1803 90afc9f3 2023-07-10 thomas error = got_error_from_errno("fclose");
1805 90afc9f3 2023-07-10 thomas if (repo)
1806 90afc9f3 2023-07-10 thomas got_repo_close(repo);
1808 90afc9f3 2023-07-10 thomas if (pack_fds) {
1809 90afc9f3 2023-07-10 thomas const struct got_error *pack_err;
1811 90afc9f3 2023-07-10 thomas pack_err = got_repo_pack_fds_close(pack_fds);
1812 90afc9f3 2023-07-10 thomas if (error == NULL)
1813 90afc9f3 2023-07-10 thomas error = pack_err;
1816 90afc9f3 2023-07-10 thomas got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1817 90afc9f3 2023-07-10 thomas got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);
1818 1ea7ccc6 2021-11-15 thomas free(repo_path);
1820 b3d68e7f 2021-07-03 stsp return error;