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 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
43 20662ea0 2021-04-10 stsp #include "got_path.h"
44 20662ea0 2021-04-10 stsp #include "got_privsep.h"
45 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
46 1ea7ccc6 2021-11-15 thomas #include "got_worktree.h"
48 20662ea0 2021-04-10 stsp #ifndef nitems
49 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
53 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
56 20662ea0 2021-04-10 stsp catch_sigint(int signo)
58 20662ea0 2021-04-10 stsp sigint_received = 1;
62 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
64 20662ea0 2021-04-10 stsp sigpipe_received = 1;
67 05118f5a 2021-06-22 stsp static const struct got_error *
68 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
70 05118f5a 2021-06-22 stsp if (sigint_received || sigpipe_received)
71 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_CANCELLED);
72 05118f5a 2021-06-22 stsp return NULL;
75 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
76 20662ea0 2021-04-10 stsp const char *cmd_name;
77 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
78 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
79 20662ea0 2021-04-10 stsp const char *cmd_alias;
82 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
83 27b10c3c 2022-07-04 thomas __dead static void usage_init(void);
84 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
85 05118f5a 2021-06-22 stsp __dead static void usage_pack(void);
86 05118f5a 2021-06-22 stsp __dead static void usage_indexpack(void);
87 05118f5a 2021-06-22 stsp __dead static void usage_listpack(void);
88 b3d68e7f 2021-07-03 stsp __dead static void usage_cleanup(void);
89 fdbea373 2023-07-10 thomas __dead static void usage_dump(void);
91 27b10c3c 2022-07-04 thomas static const struct got_error* cmd_init(int, char *[]);
92 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
93 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
94 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
95 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
96 b3d68e7f 2021-07-03 stsp static const struct got_error* cmd_cleanup(int, char *[]);
97 fdbea373 2023-07-10 thomas static const struct got_error* cmd_dump(int, char *[]);
99 641a8ee6 2022-02-16 thomas static const struct gotadmin_cmd gotadmin_commands[] = {
100 27b10c3c 2022-07-04 thomas { "init", cmd_init, usage_init, "" },
101 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
102 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
103 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
104 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
105 b3d68e7f 2021-07-03 stsp { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
106 fdbea373 2023-07-10 thomas { "dump", cmd_dump, usage_dump, "" },
109 20662ea0 2021-04-10 stsp static void
110 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
114 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
115 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
116 641a8ee6 2022-02-16 thomas const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
117 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
119 20662ea0 2021-04-10 stsp fputc('\n', fp);
123 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
125 641a8ee6 2022-02-16 thomas const struct gotadmin_cmd *cmd;
128 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
129 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
130 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
131 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
134 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
136 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
137 20662ea0 2021-04-10 stsp switch (ch) {
145 20662ea0 2021-04-10 stsp usage(hflag, 1);
146 20662ea0 2021-04-10 stsp /* NOTREACHED */
150 20662ea0 2021-04-10 stsp argc -= optind;
151 20662ea0 2021-04-10 stsp argv += optind;
152 20662ea0 2021-04-10 stsp optind = 1;
153 20662ea0 2021-04-10 stsp optreset = 1;
155 20662ea0 2021-04-10 stsp if (Vflag) {
156 20662ea0 2021-04-10 stsp got_version_print_str();
160 20662ea0 2021-04-10 stsp if (argc <= 0)
161 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
163 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
164 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
166 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
167 20662ea0 2021-04-10 stsp const struct got_error *error;
169 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
171 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
172 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
176 641a8ee6 2022-02-16 thomas cmd->cmd_usage();
178 641a8ee6 2022-02-16 thomas error = cmd->cmd_main(argc, argv);
179 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
180 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
181 20662ea0 2021-04-10 stsp !(sigpipe_received &&
182 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
183 20662ea0 2021-04-10 stsp !(sigint_received &&
184 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
185 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
192 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
193 20662ea0 2021-04-10 stsp list_commands(stderr);
197 20662ea0 2021-04-10 stsp __dead static void
198 20662ea0 2021-04-10 stsp usage(int hflag, int status)
200 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
202 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
203 20662ea0 2021-04-10 stsp getprogname());
205 20662ea0 2021-04-10 stsp list_commands(fp);
206 20662ea0 2021-04-10 stsp exit(status);
209 20662ea0 2021-04-10 stsp static const struct got_error *
210 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
212 20662ea0 2021-04-10 stsp const struct got_error *err;
214 20662ea0 2021-04-10 stsp #ifdef PROFILE
215 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
216 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
218 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
219 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
221 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
222 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
224 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
225 20662ea0 2021-04-10 stsp if (err != NULL)
226 20662ea0 2021-04-10 stsp return err;
228 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
229 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
231 20662ea0 2021-04-10 stsp return NULL;
234 20662ea0 2021-04-10 stsp __dead static void
235 20662ea0 2021-04-10 stsp usage_info(void)
237 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
238 20662ea0 2021-04-10 stsp getprogname());
242 20662ea0 2021-04-10 stsp static const struct got_error *
243 1ea7ccc6 2021-11-15 thomas get_repo_path(char **repo_path)
245 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
246 1ea7ccc6 2021-11-15 thomas struct got_worktree *worktree = NULL;
247 1ea7ccc6 2021-11-15 thomas char *cwd;
249 1ea7ccc6 2021-11-15 thomas *repo_path = NULL;
251 1ea7ccc6 2021-11-15 thomas cwd = getcwd(NULL, 0);
252 1ea7ccc6 2021-11-15 thomas if (cwd == NULL)
253 1ea7ccc6 2021-11-15 thomas return got_error_from_errno("getcwd");
255 1ea7ccc6 2021-11-15 thomas err = got_worktree_open(&worktree, cwd);
256 1ea7ccc6 2021-11-15 thomas if (err) {
257 1ea7ccc6 2021-11-15 thomas if (err->code != GOT_ERR_NOT_WORKTREE)
258 1ea7ccc6 2021-11-15 thomas goto done;
259 1ea7ccc6 2021-11-15 thomas err = NULL;
262 1ea7ccc6 2021-11-15 thomas if (worktree)
263 1ea7ccc6 2021-11-15 thomas *repo_path = strdup(got_worktree_get_repo_path(worktree));
265 1ea7ccc6 2021-11-15 thomas *repo_path = strdup(cwd);
266 1ea7ccc6 2021-11-15 thomas if (*repo_path == NULL)
267 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("strdup");
269 1ea7ccc6 2021-11-15 thomas if (worktree)
270 1ea7ccc6 2021-11-15 thomas got_worktree_close(worktree);
271 1ea7ccc6 2021-11-15 thomas free(cwd);
272 1ea7ccc6 2021-11-15 thomas return err;
275 27b10c3c 2022-07-04 thomas __dead static void
276 27b10c3c 2022-07-04 thomas usage_init(void)
278 e9424ba1 2022-09-20 thomas fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
279 e9424ba1 2022-09-20 thomas getprogname());
283 27b10c3c 2022-07-04 thomas static const struct got_error *
284 27b10c3c 2022-07-04 thomas cmd_init(int argc, char *argv[])
286 27b10c3c 2022-07-04 thomas const struct got_error *error = NULL;
287 e9424ba1 2022-09-20 thomas const char *head_name = NULL;
288 27b10c3c 2022-07-04 thomas char *repo_path = NULL;
291 a0abeae5 2023-02-17 thomas #ifndef PROFILE
292 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
293 a0abeae5 2023-02-17 thomas err(1, "pledge");
296 e9424ba1 2022-09-20 thomas while ((ch = getopt(argc, argv, "b:")) != -1) {
297 27b10c3c 2022-07-04 thomas switch (ch) {
298 e9424ba1 2022-09-20 thomas case 'b':
299 e9424ba1 2022-09-20 thomas head_name = optarg;
302 27b10c3c 2022-07-04 thomas usage_init();
303 27b10c3c 2022-07-04 thomas /* NOTREACHED */
307 27b10c3c 2022-07-04 thomas argc -= optind;
308 27b10c3c 2022-07-04 thomas argv += optind;
310 27b10c3c 2022-07-04 thomas if (argc != 1)
311 27b10c3c 2022-07-04 thomas usage_init();
313 27b10c3c 2022-07-04 thomas repo_path = strdup(argv[0]);
314 27b10c3c 2022-07-04 thomas if (repo_path == NULL)
315 27b10c3c 2022-07-04 thomas return got_error_from_errno("strdup");
317 27b10c3c 2022-07-04 thomas got_path_strip_trailing_slashes(repo_path);
319 27b10c3c 2022-07-04 thomas error = got_path_mkdir(repo_path);
320 27b10c3c 2022-07-04 thomas if (error &&
321 27b10c3c 2022-07-04 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
322 27b10c3c 2022-07-04 thomas goto done;
324 27b10c3c 2022-07-04 thomas error = apply_unveil(repo_path, 0);
325 27b10c3c 2022-07-04 thomas if (error)
326 27b10c3c 2022-07-04 thomas goto done;
328 e9424ba1 2022-09-20 thomas error = got_repo_init(repo_path, head_name);
330 27b10c3c 2022-07-04 thomas free(repo_path);
331 27b10c3c 2022-07-04 thomas return error;
334 1ea7ccc6 2021-11-15 thomas static const struct got_error *
335 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
337 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
338 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
339 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
340 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
341 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
342 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
343 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
344 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
346 a0abeae5 2023-02-17 thomas #ifndef PROFILE
347 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
348 a0abeae5 2023-02-17 thomas NULL) == -1)
349 a0abeae5 2023-02-17 thomas err(1, "pledge");
352 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
353 20662ea0 2021-04-10 stsp switch (ch) {
355 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
356 20662ea0 2021-04-10 stsp if (repo_path == NULL)
357 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
359 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
362 20662ea0 2021-04-10 stsp usage_info();
363 20662ea0 2021-04-10 stsp /* NOTREACHED */
367 20662ea0 2021-04-10 stsp argc -= optind;
368 20662ea0 2021-04-10 stsp argv += optind;
370 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
371 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
372 1ea7ccc6 2021-11-15 thomas if (error)
373 1ea7ccc6 2021-11-15 thomas goto done;
375 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
376 7cd52833 2022-06-23 thomas if (error != NULL)
377 7cd52833 2022-06-23 thomas goto done;
378 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
381 bfb5ee0b 2022-05-31 thomas #ifndef PROFILE
382 bfb5ee0b 2022-05-31 thomas /* Remove "cpath" promise. */
383 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
384 bfb5ee0b 2022-05-31 thomas NULL) == -1)
385 bfb5ee0b 2022-05-31 thomas err(1, "pledge");
387 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
391 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
393 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
394 20662ea0 2021-04-10 stsp if (gotconfig) {
395 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
396 20662ea0 2021-04-10 stsp int i, nremotes;
397 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
398 20662ea0 2021-04-10 stsp printf("default author: %s\n",
399 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
401 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
402 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
403 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
404 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
405 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
406 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
407 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
409 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
410 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
411 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
412 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
417 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
418 20662ea0 2021-04-10 stsp &packsize, repo);
421 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
422 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
423 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
424 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
427 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
428 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
431 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
434 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
435 20662ea0 2021-04-10 stsp if (nobj > 0) {
436 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
437 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
440 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
444 20662ea0 2021-04-10 stsp got_repo_close(repo);
445 7cd52833 2022-06-23 thomas if (pack_fds) {
446 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
447 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
448 7cd52833 2022-06-23 thomas if (error == NULL)
449 7cd52833 2022-06-23 thomas error = pack_err;
452 1ea7ccc6 2021-11-15 thomas free(repo_path);
453 20662ea0 2021-04-10 stsp return error;
456 05118f5a 2021-06-22 stsp __dead static void
457 05118f5a 2021-06-22 stsp usage_pack(void)
459 d8253374 2023-02-17 thomas fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
460 d6506a3d 2022-08-16 thomas "[-x reference] [reference ...]\n", getprogname());
464 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
465 fdbea373 2023-07-10 thomas FILE *out;
466 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
467 85220b0e 2022-03-22 thomas int last_ncolored;
468 85220b0e 2022-03-22 thomas int last_nfound;
469 85220b0e 2022-03-22 thomas int last_ntrees;
470 85220b0e 2022-03-22 thomas int loading_done;
471 05118f5a 2021-06-22 stsp int last_ncommits;
472 05118f5a 2021-06-22 stsp int last_nobj_total;
473 05118f5a 2021-06-22 stsp int last_p_deltify;
474 05118f5a 2021-06-22 stsp int last_p_written;
475 05118f5a 2021-06-22 stsp int last_p_indexed;
476 05118f5a 2021-06-22 stsp int last_p_resolved;
477 05118f5a 2021-06-22 stsp int verbosity;
478 05118f5a 2021-06-22 stsp int printed_something;
481 85220b0e 2022-03-22 thomas static void
482 fdbea373 2023-07-10 thomas print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
483 85220b0e 2022-03-22 thomas int ncolored, int nfound, int ntrees)
485 85220b0e 2022-03-22 thomas if (print_colored) {
486 fdbea373 2023-07-10 thomas fprintf(out, "%d commit%s colored", ncolored,
487 85220b0e 2022-03-22 thomas ncolored == 1 ? "" : "s");
489 85220b0e 2022-03-22 thomas if (print_found) {
490 fdbea373 2023-07-10 thomas fprintf(out, "%s%d object%s found",
491 85220b0e 2022-03-22 thomas ncolored > 0 ? "; " : "",
492 85220b0e 2022-03-22 thomas nfound, nfound == 1 ? "" : "s");
494 85220b0e 2022-03-22 thomas if (print_trees) {
495 fdbea373 2023-07-10 thomas fprintf(out, "; %d tree%s scanned", ntrees,
496 85220b0e 2022-03-22 thomas ntrees == 1 ? "" : "s");
500 05118f5a 2021-06-22 stsp static const struct got_error *
501 85220b0e 2022-03-22 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
502 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
503 85220b0e 2022-03-22 thomas int nobj_written)
505 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
506 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
507 05118f5a 2021-06-22 stsp int p_deltify, p_written;
508 85220b0e 2022-03-22 thomas int print_colored = 0, print_found = 0, print_trees = 0;
509 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
510 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
512 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
513 85220b0e 2022-03-22 thomas return NULL;
515 85220b0e 2022-03-22 thomas if (a->last_ncolored != ncolored) {
516 85220b0e 2022-03-22 thomas print_colored = 1;
517 85220b0e 2022-03-22 thomas a->last_ncolored = ncolored;
520 85220b0e 2022-03-22 thomas if (a->last_nfound != nfound) {
521 85220b0e 2022-03-22 thomas print_colored = 1;
522 85220b0e 2022-03-22 thomas print_found = 1;
523 85220b0e 2022-03-22 thomas a->last_nfound = nfound;
526 85220b0e 2022-03-22 thomas if (a->last_ntrees != ntrees) {
527 85220b0e 2022-03-22 thomas print_colored = 1;
528 85220b0e 2022-03-22 thomas print_found = 1;
529 85220b0e 2022-03-22 thomas print_trees = 1;
530 85220b0e 2022-03-22 thomas a->last_ntrees = ntrees;
533 85220b0e 2022-03-22 thomas if ((print_colored || print_found || print_trees) &&
534 85220b0e 2022-03-22 thomas !a->loading_done) {
535 fdbea373 2023-07-10 thomas fprintf(a->out, "\r");
536 fdbea373 2023-07-10 thomas print_load_info(a->out, print_colored, print_found,
537 fdbea373 2023-07-10 thomas print_trees, ncolored, nfound, ntrees);
538 85220b0e 2022-03-22 thomas a->printed_something = 1;
539 fdbea373 2023-07-10 thomas fflush(a->out);
540 05118f5a 2021-06-22 stsp return NULL;
541 85220b0e 2022-03-22 thomas } else if (!a->loading_done) {
542 fdbea373 2023-07-10 thomas fprintf(a->out, "\r");
543 fdbea373 2023-07-10 thomas print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
544 fdbea373 2023-07-10 thomas fprintf(a->out, "\n");
545 85220b0e 2022-03-22 thomas a->loading_done = 1;
548 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
549 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
551 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
552 05118f5a 2021-06-22 stsp print_searching = 1;
553 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
556 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
557 05118f5a 2021-06-22 stsp print_searching = 1;
558 05118f5a 2021-06-22 stsp print_total = 1;
559 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
562 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
563 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
564 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
565 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
566 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
569 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
570 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
571 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
572 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
573 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
574 05118f5a 2021-06-22 stsp print_searching = 1;
575 05118f5a 2021-06-22 stsp print_total = 1;
576 05118f5a 2021-06-22 stsp print_deltify = 1;
579 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
580 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
581 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
582 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
583 05118f5a 2021-06-22 stsp print_searching = 1;
584 05118f5a 2021-06-22 stsp print_total = 1;
585 05118f5a 2021-06-22 stsp print_deltify = 1;
586 05118f5a 2021-06-22 stsp print_written = 1;
591 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
592 fdbea373 2023-07-10 thomas fprintf(a->out, "\r");
593 05118f5a 2021-06-22 stsp if (print_searching)
594 fdbea373 2023-07-10 thomas fprintf(a->out, "packing %d reference%s", ncommits,
595 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
596 05118f5a 2021-06-22 stsp if (print_total)
597 fdbea373 2023-07-10 thomas fprintf(a->out, "; %d object%s", nobj_total,
598 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
599 05118f5a 2021-06-22 stsp if (print_deltify)
600 fdbea373 2023-07-10 thomas fprintf(a->out, "; deltify: %d%%", p_deltify);
601 05118f5a 2021-06-22 stsp if (print_written)
602 fdbea373 2023-07-10 thomas fprintf(a->out, "; writing pack: %*s %d%%",
603 fdbea373 2023-07-10 thomas FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
604 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
605 05118f5a 2021-06-22 stsp print_written) {
606 05118f5a 2021-06-22 stsp a->printed_something = 1;
607 fdbea373 2023-07-10 thomas fflush(a->out);
609 05118f5a 2021-06-22 stsp return NULL;
612 05118f5a 2021-06-22 stsp static const struct got_error *
613 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
614 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
616 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
617 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
618 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
619 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
621 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
622 05118f5a 2021-06-22 stsp return NULL;
624 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
625 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
626 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
627 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
628 05118f5a 2021-06-22 stsp print_size = 1;
629 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
630 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
631 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
633 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
634 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
635 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
636 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
637 05118f5a 2021-06-22 stsp print_indexed = 1;
638 05118f5a 2021-06-22 stsp print_size = 1;
641 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
642 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
643 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
644 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
645 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
646 05118f5a 2021-06-22 stsp print_resolved = 1;
647 05118f5a 2021-06-22 stsp print_indexed = 1;
648 05118f5a 2021-06-22 stsp print_size = 1;
653 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
654 05118f5a 2021-06-22 stsp printf("\r");
655 05118f5a 2021-06-22 stsp if (print_size)
656 d2f35ef7 2022-02-13 thomas printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
657 05118f5a 2021-06-22 stsp if (print_indexed)
658 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
659 05118f5a 2021-06-22 stsp if (print_resolved)
660 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
661 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
662 05118f5a 2021-06-22 stsp fflush(stdout);
664 05118f5a 2021-06-22 stsp return NULL;
667 05118f5a 2021-06-22 stsp static const struct got_error *
668 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
669 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
671 05118f5a 2021-06-22 stsp const struct got_error *err;
672 05118f5a 2021-06-22 stsp struct got_reference *ref;
674 05118f5a 2021-06-22 stsp *new = NULL;
676 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
678 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
679 05118f5a 2021-06-22 stsp return err;
681 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
682 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
683 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
685 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
686 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
687 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
688 05118f5a 2021-06-22 stsp got_ref_close(ref);
691 05118f5a 2021-06-22 stsp return err;
694 05118f5a 2021-06-22 stsp static const struct got_error *
695 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
697 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
698 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
699 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
700 d8253374 2023-02-17 thomas int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
701 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
702 05118f5a 2021-06-22 stsp char *id_str = NULL;
703 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
704 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
705 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
706 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
707 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
708 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
709 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
710 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
712 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
713 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
714 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
716 a0abeae5 2023-02-17 thomas #ifndef PROFILE
717 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
718 a0abeae5 2023-02-17 thomas NULL) == -1)
719 a0abeae5 2023-02-17 thomas err(1, "pledge");
722 d8253374 2023-02-17 thomas while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
723 05118f5a 2021-06-22 stsp switch (ch) {
725 05118f5a 2021-06-22 stsp loose_obj_only = 0;
727 d8253374 2023-02-17 thomas case 'D':
728 d8253374 2023-02-17 thomas force_refdelta = 1;
730 f7065961 2022-10-27 thomas case 'q':
731 f7065961 2022-10-27 thomas verbosity = -1;
734 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
735 05118f5a 2021-06-22 stsp if (repo_path == NULL)
736 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
738 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
741 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
742 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
743 05118f5a 2021-06-22 stsp optarg, NULL);
745 05118f5a 2021-06-22 stsp return error;
748 05118f5a 2021-06-22 stsp usage_pack();
749 05118f5a 2021-06-22 stsp /* NOTREACHED */
753 05118f5a 2021-06-22 stsp argc -= optind;
754 05118f5a 2021-06-22 stsp argv += optind;
756 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
757 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
758 1ea7ccc6 2021-11-15 thomas if (error)
759 1ea7ccc6 2021-11-15 thomas goto done;
761 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
762 7cd52833 2022-06-23 thomas if (error != NULL)
763 7cd52833 2022-06-23 thomas goto done;
764 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
768 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
772 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
773 05118f5a 2021-06-22 stsp const char *refname = pe->path;
774 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
779 05118f5a 2021-06-22 stsp if (argc == 0) {
780 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
781 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
785 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
786 05118f5a 2021-06-22 stsp const char *refname;
787 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
788 05118f5a 2021-06-22 stsp refname = argv[i];
789 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
795 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
796 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
797 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
798 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
800 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
801 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
805 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
806 fdbea373 2023-07-10 thomas ppa.out = stdout;
807 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
808 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
809 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
810 c8fc6d14 2022-04-16 thomas ppa.verbosity = verbosity;
812 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
813 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
814 d8253374 2023-02-17 thomas force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
815 05118f5a 2021-06-22 stsp if (error) {
816 05118f5a 2021-06-22 stsp if (ppa.printed_something)
817 05118f5a 2021-06-22 stsp printf("\n");
821 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
824 c8fc6d14 2022-04-16 thomas if (verbosity >= 0)
825 c8fc6d14 2022-04-16 thomas printf("\nWrote %s.pack\n", id_str);
827 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
828 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
831 c8fc6d14 2022-04-16 thomas if (verbosity >= 0)
832 c8fc6d14 2022-04-16 thomas printf("\nIndexed %s.pack\n", id_str);
834 c75a6067 2021-10-15 thomas if (repo)
835 c75a6067 2021-10-15 thomas got_repo_close(repo);
836 7cd52833 2022-06-23 thomas if (pack_fds) {
837 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
838 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
839 7cd52833 2022-06-23 thomas if (error == NULL)
840 7cd52833 2022-06-23 thomas error = pack_err;
842 21c2d8be 2023-01-10 thomas got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
843 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
844 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
845 05118f5a 2021-06-22 stsp free(id_str);
846 05118f5a 2021-06-22 stsp free(pack_hash);
847 1ea7ccc6 2021-11-15 thomas free(repo_path);
848 05118f5a 2021-06-22 stsp return error;
851 05118f5a 2021-06-22 stsp __dead static void
852 05118f5a 2021-06-22 stsp usage_indexpack(void)
854 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
855 05118f5a 2021-06-22 stsp getprogname());
859 05118f5a 2021-06-22 stsp static const struct got_error *
860 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
862 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
863 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
865 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
866 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
867 05118f5a 2021-06-22 stsp char *id_str = NULL;
868 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
869 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
870 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
872 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
873 05118f5a 2021-06-22 stsp switch (ch) {
875 05118f5a 2021-06-22 stsp usage_indexpack();
876 05118f5a 2021-06-22 stsp /* NOTREACHED */
880 05118f5a 2021-06-22 stsp argc -= optind;
881 05118f5a 2021-06-22 stsp argv += optind;
883 05118f5a 2021-06-22 stsp if (argc != 1)
884 05118f5a 2021-06-22 stsp usage_indexpack();
886 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
887 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
888 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
890 05118f5a 2021-06-22 stsp #ifndef PROFILE
891 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
892 05118f5a 2021-06-22 stsp NULL) == -1)
893 05118f5a 2021-06-22 stsp err(1, "pledge");
896 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
897 7cd52833 2022-06-23 thomas if (error != NULL)
898 7cd52833 2022-06-23 thomas goto done;
899 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
903 001a95cd 2021-10-15 thomas error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
907 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
908 fdbea373 2023-07-10 thomas ppa.out = stdout;
909 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
910 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
911 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
913 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
914 05118f5a 2021-06-22 stsp packfile_path);
918 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
922 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
923 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
926 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
928 c75a6067 2021-10-15 thomas if (repo)
929 c75a6067 2021-10-15 thomas got_repo_close(repo);
930 7cd52833 2022-06-23 thomas if (pack_fds) {
931 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
932 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
933 7cd52833 2022-06-23 thomas if (error == NULL)
934 7cd52833 2022-06-23 thomas error = pack_err;
936 05118f5a 2021-06-22 stsp free(id_str);
937 05118f5a 2021-06-22 stsp free(pack_hash);
938 05118f5a 2021-06-22 stsp return error;
941 05118f5a 2021-06-22 stsp __dead static void
942 05118f5a 2021-06-22 stsp usage_listpack(void)
944 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
945 05118f5a 2021-06-22 stsp getprogname());
949 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
950 05118f5a 2021-06-22 stsp int nblobs;
951 05118f5a 2021-06-22 stsp int ntrees;
952 05118f5a 2021-06-22 stsp int ncommits;
954 05118f5a 2021-06-22 stsp int noffdeltas;
955 05118f5a 2021-06-22 stsp int nrefdeltas;
956 05118f5a 2021-06-22 stsp int human_readable;
959 05118f5a 2021-06-22 stsp static const struct got_error *
960 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
961 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
963 05118f5a 2021-06-22 stsp const struct got_error *err;
964 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
965 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
966 05118f5a 2021-06-22 stsp const char *type_str;
968 7cd52833 2022-06-23 thomas err = got_object_id_str(&id_str, id);
970 05118f5a 2021-06-22 stsp return err;
972 05118f5a 2021-06-22 stsp switch (type) {
973 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
974 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
975 05118f5a 2021-06-22 stsp a->nblobs++;
977 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
978 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
979 05118f5a 2021-06-22 stsp a->ntrees++;
981 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
982 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
983 05118f5a 2021-06-22 stsp a->ncommits++;
985 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
986 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
987 05118f5a 2021-06-22 stsp a->ntags++;
989 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
990 05118f5a 2021-06-22 stsp type_str = "offset-delta";
991 c414a013 2021-09-25 thomas.ad if (asprintf(&delta_str, " base-offset %lld",
992 c414a013 2021-09-25 thomas.ad (long long)base_offset) == -1) {
993 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
996 05118f5a 2021-06-22 stsp a->noffdeltas++;
998 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
999 05118f5a 2021-06-22 stsp type_str = "ref-delta";
1000 b6b86fd1 2022-08-30 thomas err = got_object_id_str(&base_id_str, base_id);
1002 05118f5a 2021-06-22 stsp goto done;
1003 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1004 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
1005 05118f5a 2021-06-22 stsp goto done;
1007 05118f5a 2021-06-22 stsp a->nrefdeltas++;
1010 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1011 05118f5a 2021-06-22 stsp goto done;
1013 05118f5a 2021-06-22 stsp if (a->human_readable) {
1014 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
1016 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
1017 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
1018 05118f5a 2021-06-22 stsp goto done;
1020 05118f5a 2021-06-22 stsp s = scaled;
1021 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
1023 c414a013 2021-09-25 thomas.ad printf("%s %s at %lld size %s%s\n", id_str, type_str,
1024 c414a013 2021-09-25 thomas.ad (long long)offset, s, delta_str ? delta_str : "");
1026 c414a013 2021-09-25 thomas.ad printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1027 c414a013 2021-09-25 thomas.ad (long long)offset, (long long)size,
1028 c414a013 2021-09-25 thomas.ad delta_str ? delta_str : "");
1031 05118f5a 2021-06-22 stsp free(id_str);
1032 05118f5a 2021-06-22 stsp free(base_id_str);
1033 05118f5a 2021-06-22 stsp free(delta_str);
1034 05118f5a 2021-06-22 stsp return err;
1037 05118f5a 2021-06-22 stsp static const struct got_error *
1038 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
1040 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
1041 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
1043 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
1044 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
1045 05118f5a 2021-06-22 stsp char *id_str = NULL;
1046 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
1047 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
1048 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
1049 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
1051 a0abeae5 2023-02-17 thomas #ifndef PROFILE
1052 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1053 a0abeae5 2023-02-17 thomas NULL) == -1)
1054 a0abeae5 2023-02-17 thomas err(1, "pledge");
1057 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
1058 05118f5a 2021-06-22 stsp switch (ch) {
1060 05118f5a 2021-06-22 stsp human_readable = 1;
1063 05118f5a 2021-06-22 stsp show_stats = 1;
1066 05118f5a 2021-06-22 stsp usage_listpack();
1067 05118f5a 2021-06-22 stsp /* NOTREACHED */
1071 05118f5a 2021-06-22 stsp argc -= optind;
1072 05118f5a 2021-06-22 stsp argv += optind;
1074 05118f5a 2021-06-22 stsp if (argc != 1)
1075 05118f5a 2021-06-22 stsp usage_listpack();
1076 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
1077 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
1078 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
1080 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1081 7cd52833 2022-06-23 thomas if (error != NULL)
1082 7cd52833 2022-06-23 thomas goto done;
1083 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1084 05118f5a 2021-06-22 stsp if (error)
1085 05118f5a 2021-06-22 stsp goto done;
1086 bfb5ee0b 2022-05-31 thomas #ifndef PROFILE
1087 bfb5ee0b 2022-05-31 thomas /* Remove "cpath" promise. */
1088 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1089 bfb5ee0b 2022-05-31 thomas NULL) == -1)
1090 bfb5ee0b 2022-05-31 thomas err(1, "pledge");
1092 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1093 05118f5a 2021-06-22 stsp if (error)
1094 05118f5a 2021-06-22 stsp goto done;
1096 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
1097 05118f5a 2021-06-22 stsp packfile_path);
1098 05118f5a 2021-06-22 stsp if (error)
1099 05118f5a 2021-06-22 stsp goto done;
1100 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
1101 05118f5a 2021-06-22 stsp if (error)
1102 05118f5a 2021-06-22 stsp goto done;
1104 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
1105 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
1106 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
1107 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
1108 05118f5a 2021-06-22 stsp if (error)
1109 05118f5a 2021-06-22 stsp goto done;
1110 05118f5a 2021-06-22 stsp if (show_stats) {
1111 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1112 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1113 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1114 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
1115 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1116 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
1119 c75a6067 2021-10-15 thomas if (repo)
1120 c75a6067 2021-10-15 thomas got_repo_close(repo);
1121 7cd52833 2022-06-23 thomas if (pack_fds) {
1122 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
1123 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
1124 7cd52833 2022-06-23 thomas if (error == NULL)
1125 7cd52833 2022-06-23 thomas error = pack_err;
1127 05118f5a 2021-06-22 stsp free(id_str);
1128 05118f5a 2021-06-22 stsp free(pack_hash);
1129 05118f5a 2021-06-22 stsp free(packfile_path);
1130 05118f5a 2021-06-22 stsp return error;
1133 b3d68e7f 2021-07-03 stsp __dead static void
1134 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1136 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1137 d6506a3d 2022-08-16 thomas getprogname());
1141 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1142 b3d68e7f 2021-07-03 stsp int last_nloose;
1143 b3d68e7f 2021-07-03 stsp int last_ncommits;
1144 b3d68e7f 2021-07-03 stsp int last_npurged;
1145 afd0da38 2023-06-22 thomas int last_nredundant;
1146 b3d68e7f 2021-07-03 stsp int verbosity;
1147 b3d68e7f 2021-07-03 stsp int printed_something;
1148 b3d68e7f 2021-07-03 stsp int dry_run;
1151 b3d68e7f 2021-07-03 stsp static const struct got_error *
1152 afd0da38 2023-06-22 thomas cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1153 afd0da38 2023-06-22 thomas int nredundant)
1155 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1156 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1157 afd0da38 2023-06-22 thomas int print_redundant = 0;
1159 b3d68e7f 2021-07-03 stsp if (a->last_nloose != nloose) {
1160 b3d68e7f 2021-07-03 stsp print_loose = 1;
1161 b3d68e7f 2021-07-03 stsp a->last_nloose = nloose;
1163 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1164 b3d68e7f 2021-07-03 stsp print_loose = 1;
1165 b3d68e7f 2021-07-03 stsp print_commits = 1;
1166 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1168 b3d68e7f 2021-07-03 stsp if (a->last_npurged != npurged) {
1169 b3d68e7f 2021-07-03 stsp print_loose = 1;
1170 b3d68e7f 2021-07-03 stsp print_commits = 1;
1171 b3d68e7f 2021-07-03 stsp print_purged = 1;
1172 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1174 afd0da38 2023-06-22 thomas if (a->last_nredundant != nredundant) {
1175 91554d23 2023-06-25 thomas print_loose = 1;
1176 91554d23 2023-06-25 thomas print_commits = 1;
1177 91554d23 2023-06-25 thomas print_purged = 1;
1178 afd0da38 2023-06-22 thomas print_redundant = 1;
1179 afd0da38 2023-06-22 thomas a->last_nredundant = nredundant;
1182 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1183 b3d68e7f 2021-07-03 stsp return NULL;
1185 afd0da38 2023-06-22 thomas if (print_loose || print_commits || print_purged || print_redundant)
1186 b3d68e7f 2021-07-03 stsp printf("\r");
1187 b3d68e7f 2021-07-03 stsp if (print_loose)
1188 b3d68e7f 2021-07-03 stsp printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1189 b3d68e7f 2021-07-03 stsp if (print_commits)
1190 b3d68e7f 2021-07-03 stsp printf("; %d commit%s scanned", ncommits,
1191 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1192 91554d23 2023-06-25 thomas if (print_purged || print_redundant) {
1193 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1194 91554d23 2023-06-25 thomas printf("; could purge %d object%s", npurged,
1195 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1197 91554d23 2023-06-25 thomas printf("; purged %d object%s", npurged,
1198 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1201 afd0da38 2023-06-22 thomas if (print_redundant) {
1202 afd0da38 2023-06-22 thomas if (a->dry_run) {
1203 91554d23 2023-06-25 thomas printf(", %d pack file%s", nredundant,
1204 afd0da38 2023-06-22 thomas nredundant == 1 ? "" : "s");
1205 afd0da38 2023-06-22 thomas } else {
1206 91554d23 2023-06-25 thomas printf(", %d pack file%s", nredundant,
1207 afd0da38 2023-06-22 thomas nredundant == 1 ? "" : "s");
1210 afd0da38 2023-06-22 thomas if (print_loose || print_commits || print_purged || print_redundant) {
1211 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1212 b3d68e7f 2021-07-03 stsp fflush(stdout);
1214 b3d68e7f 2021-07-03 stsp return NULL;
1217 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1218 1124fe40 2021-07-07 stsp int verbosity;
1219 1124fe40 2021-07-07 stsp int printed_something;
1220 1124fe40 2021-07-07 stsp int dry_run;
1223 b3d68e7f 2021-07-03 stsp static const struct got_error *
1224 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1226 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1228 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1229 1124fe40 2021-07-07 stsp return NULL;
1231 1124fe40 2021-07-07 stsp if (a->dry_run)
1232 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1234 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1236 1124fe40 2021-07-07 stsp a->printed_something = 1;
1237 1124fe40 2021-07-07 stsp return NULL;
1240 1124fe40 2021-07-07 stsp static const struct got_error *
1241 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1243 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1244 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
1245 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1246 91554d23 2023-06-25 thomas int ch, dry_run = 0, verbosity = 0;
1247 91554d23 2023-06-25 thomas int ncommits = 0, nloose = 0, npacked = 0;
1248 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1249 e81b604b 2023-06-22 thomas struct got_lockfile *lock = NULL;
1250 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1251 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1252 afd0da38 2023-06-22 thomas off_t loose_before, loose_after;
1253 afd0da38 2023-06-22 thomas off_t pack_before, pack_after;
1254 afd0da38 2023-06-22 thomas off_t total_size;
1255 afd0da38 2023-06-22 thomas char loose_before_scaled[FMT_SCALED_STRSIZE];
1256 afd0da38 2023-06-22 thomas char loose_after_scaled[FMT_SCALED_STRSIZE];
1257 afd0da38 2023-06-22 thomas char pack_before_scaled[FMT_SCALED_STRSIZE];
1258 afd0da38 2023-06-22 thomas char pack_after_scaled[FMT_SCALED_STRSIZE];
1259 afd0da38 2023-06-22 thomas char total_size_scaled[FMT_SCALED_STRSIZE];
1260 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
1262 a0abeae5 2023-02-17 thomas #ifndef PROFILE
1263 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1264 a0abeae5 2023-02-17 thomas NULL) == -1)
1265 a0abeae5 2023-02-17 thomas err(1, "pledge");
1268 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1269 b3d68e7f 2021-07-03 stsp switch (ch) {
1271 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1273 f7065961 2022-10-27 thomas case 'n':
1274 f7065961 2022-10-27 thomas dry_run = 1;
1277 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1279 f7065961 2022-10-27 thomas case 'q':
1280 f7065961 2022-10-27 thomas verbosity = -1;
1283 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1284 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1285 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1287 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1290 b3d68e7f 2021-07-03 stsp usage_cleanup();
1291 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1295 b3d68e7f 2021-07-03 stsp argc -= optind;
1296 b3d68e7f 2021-07-03 stsp argv += optind;
1298 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
1299 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
1300 1ea7ccc6 2021-11-15 thomas if (error)
1301 1ea7ccc6 2021-11-15 thomas goto done;
1303 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1304 7cd52833 2022-06-23 thomas if (error != NULL)
1305 7cd52833 2022-06-23 thomas goto done;
1306 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1307 b3d68e7f 2021-07-03 stsp if (error)
1308 b3d68e7f 2021-07-03 stsp goto done;
1310 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1311 b3d68e7f 2021-07-03 stsp if (error)
1312 b3d68e7f 2021-07-03 stsp goto done;
1314 2624d165 2023-02-07 thomas if (got_repo_has_extension(repo, "preciousObjects")) {
1315 2624d165 2023-02-07 thomas error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1316 2624d165 2023-02-07 thomas "the preciousObjects Git extension is enabled; "
1317 2624d165 2023-02-07 thomas "this implies that objects must not be deleted");
1318 2624d165 2023-02-07 thomas goto done;
1321 e81b604b 2023-06-22 thomas error = got_repo_cleanup_prepare(repo, &lock);
1322 e81b604b 2023-06-22 thomas if (error)
1323 e81b604b 2023-06-22 thomas goto done;
1325 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1326 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1327 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1328 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1329 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1330 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1331 1124fe40 2021-07-07 stsp goto done;
1334 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1335 b3d68e7f 2021-07-03 stsp cpa.last_ncommits = -1;
1336 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1337 afd0da38 2023-06-22 thomas cpa.last_nredundant = -1;
1338 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1339 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1341 b3d68e7f 2021-07-03 stsp error = got_repo_purge_unreferenced_loose_objects(repo,
1342 91554d23 2023-06-25 thomas &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1343 91554d23 2023-06-25 thomas dry_run, ignore_mtime, cleanup_progress, &cpa,
1344 91554d23 2023-06-25 thomas check_cancelled, NULL);
1345 91554d23 2023-06-25 thomas if (error) {
1346 91554d23 2023-06-25 thomas if (cpa.printed_something)
1347 91554d23 2023-06-25 thomas printf("\n");
1348 91554d23 2023-06-25 thomas goto done;
1351 91554d23 2023-06-25 thomas error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1352 91554d23 2023-06-25 thomas &pack_after, dry_run, ncommits, nloose, npacked,
1353 abc59930 2021-09-05 naddy cleanup_progress, &cpa, check_cancelled, NULL);
1354 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1355 b3d68e7f 2021-07-03 stsp printf("\n");
1356 afd0da38 2023-06-22 thomas if (error)
1357 afd0da38 2023-06-22 thomas goto done;
1359 afd0da38 2023-06-22 thomas total_size = (loose_before - loose_after) + (pack_before - pack_after);
1361 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1362 afd0da38 2023-06-22 thomas if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1363 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1364 b3d68e7f 2021-07-03 stsp goto done;
1366 afd0da38 2023-06-22 thomas if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1367 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1368 b3d68e7f 2021-07-03 stsp goto done;
1370 afd0da38 2023-06-22 thomas if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1371 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1372 b3d68e7f 2021-07-03 stsp goto done;
1374 afd0da38 2023-06-22 thomas if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1375 afd0da38 2023-06-22 thomas error = got_error_from_errno("fmt_scaled");
1376 afd0da38 2023-06-22 thomas goto done;
1378 afd0da38 2023-06-22 thomas if (fmt_scaled(total_size, total_size_scaled) == -1) {
1379 afd0da38 2023-06-22 thomas error = got_error_from_errno("fmt_scaled");
1380 afd0da38 2023-06-22 thomas goto done;
1382 afd0da38 2023-06-22 thomas printf("loose total size before: %s\n", loose_before_scaled);
1383 afd0da38 2023-06-22 thomas printf("loose total size after: %s\n", loose_after_scaled);
1384 afd0da38 2023-06-22 thomas printf("pack files total size before: %s\n",
1385 afd0da38 2023-06-22 thomas pack_before_scaled);
1386 afd0da38 2023-06-22 thomas printf("pack files total size after: %s\n", pack_after_scaled);
1387 b3d68e7f 2021-07-03 stsp if (dry_run) {
1388 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1389 afd0da38 2023-06-22 thomas total_size_scaled);
1391 afd0da38 2023-06-22 thomas printf("disk space freed: %s\n", total_size_scaled);
1392 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1396 e81b604b 2023-06-22 thomas got_repo_cleanup_complete(repo, lock);
1398 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1399 7cd52833 2022-06-23 thomas if (pack_fds) {
1400 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
1401 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
1402 fdbea373 2023-07-10 thomas if (error == NULL)
1403 fdbea373 2023-07-10 thomas error = pack_err;
1405 fdbea373 2023-07-10 thomas free(repo_path);
1406 fdbea373 2023-07-10 thomas return error;
1409 fdbea373 2023-07-10 thomas __dead static void
1410 fdbea373 2023-07-10 thomas usage_dump(void)
1412 fdbea373 2023-07-10 thomas fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1413 fdbea373 2023-07-10 thomas "[-x reference] [reference]...\n", getprogname());
1414 fdbea373 2023-07-10 thomas exit(1);
1417 fdbea373 2023-07-10 thomas static const struct got_error *
1418 fdbea373 2023-07-10 thomas cmd_dump(int argc, char *argv[])
1420 fdbea373 2023-07-10 thomas const struct got_error *error = NULL;
1421 fdbea373 2023-07-10 thomas struct got_pack_progress_arg ppa;
1422 fdbea373 2023-07-10 thomas struct got_repository *repo = NULL;
1423 fdbea373 2023-07-10 thomas struct got_pathlist_head exclude_args;
1424 fdbea373 2023-07-10 thomas struct got_pathlist_entry *pe;
1425 fdbea373 2023-07-10 thomas struct got_reflist_head exclude_refs;
1426 fdbea373 2023-07-10 thomas struct got_reflist_head include_refs;
1427 fdbea373 2023-07-10 thomas struct got_reflist_entry *re, *new;
1428 fdbea373 2023-07-10 thomas const char *refname;
1429 fdbea373 2023-07-10 thomas char *repo_path = NULL;
1430 fdbea373 2023-07-10 thomas int *pack_fds = NULL;
1431 fdbea373 2023-07-10 thomas int verbosity = 0;
1432 fdbea373 2023-07-10 thomas int i, ch;
1434 fdbea373 2023-07-10 thomas TAILQ_INIT(&exclude_args);
1435 fdbea373 2023-07-10 thomas TAILQ_INIT(&exclude_refs);
1436 fdbea373 2023-07-10 thomas TAILQ_INIT(&include_refs);
1438 fdbea373 2023-07-10 thomas #ifndef PROFILE
1439 fdbea373 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1440 fdbea373 2023-07-10 thomas NULL) == -1)
1441 fdbea373 2023-07-10 thomas err(1, "pledge");
1444 fdbea373 2023-07-10 thomas while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1445 fdbea373 2023-07-10 thomas switch (ch) {
1446 fdbea373 2023-07-10 thomas case 'q':
1447 fdbea373 2023-07-10 thomas verbosity = -1;
1449 fdbea373 2023-07-10 thomas case 'r':
1450 fdbea373 2023-07-10 thomas repo_path = realpath(optarg, NULL);
1451 fdbea373 2023-07-10 thomas if (repo_path == NULL)
1452 fdbea373 2023-07-10 thomas return got_error_from_errno2("realpath",
1453 fdbea373 2023-07-10 thomas optarg);
1454 fdbea373 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
1456 fdbea373 2023-07-10 thomas case 'x':
1457 fdbea373 2023-07-10 thomas error = got_pathlist_append(&exclude_args,
1458 fdbea373 2023-07-10 thomas optarg, NULL);
1459 fdbea373 2023-07-10 thomas if (error)
1460 fdbea373 2023-07-10 thomas return error;
1462 fdbea373 2023-07-10 thomas default:
1463 fdbea373 2023-07-10 thomas usage_dump();
1464 fdbea373 2023-07-10 thomas /* NOTREACHED */
1467 fdbea373 2023-07-10 thomas argc -= optind;
1468 fdbea373 2023-07-10 thomas argv += optind;
1470 fdbea373 2023-07-10 thomas if (repo_path == NULL) {
1471 fdbea373 2023-07-10 thomas error = get_repo_path(&repo_path);
1472 fdbea373 2023-07-10 thomas if (error)
1473 fdbea373 2023-07-10 thomas goto done;
1475 fdbea373 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1476 fdbea373 2023-07-10 thomas if (error != NULL)
1477 fdbea373 2023-07-10 thomas goto done;
1478 fdbea373 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1479 fdbea373 2023-07-10 thomas if (error)
1480 fdbea373 2023-07-10 thomas goto done;
1482 fdbea373 2023-07-10 thomas error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1483 fdbea373 2023-07-10 thomas if (error)
1484 fdbea373 2023-07-10 thomas goto done;
1486 fdbea373 2023-07-10 thomas TAILQ_FOREACH(pe, &exclude_args, entry) {
1487 fdbea373 2023-07-10 thomas refname = pe->path;
1488 fdbea373 2023-07-10 thomas error = add_ref(&new, &exclude_refs, refname, repo);
1489 fdbea373 2023-07-10 thomas if (error)
1490 fdbea373 2023-07-10 thomas goto done;
1493 fdbea373 2023-07-10 thomas if (argc == 0) {
1494 fdbea373 2023-07-10 thomas error = got_ref_list(&include_refs, repo, "",
1495 fdbea373 2023-07-10 thomas got_ref_cmp_by_name, NULL);
1496 fdbea373 2023-07-10 thomas if (error)
1497 fdbea373 2023-07-10 thomas goto done;
1498 fdbea373 2023-07-10 thomas } else {
1499 fdbea373 2023-07-10 thomas for (i = 0; i < argc; i++) {
1500 fdbea373 2023-07-10 thomas got_path_strip_trailing_slashes(argv[i]);
1501 fdbea373 2023-07-10 thomas refname = argv[i];
1502 fdbea373 2023-07-10 thomas error = add_ref(&new, &include_refs, refname, repo);
1503 fdbea373 2023-07-10 thomas if (error)
1504 fdbea373 2023-07-10 thomas goto done;
1508 fdbea373 2023-07-10 thomas /* Ignore references in the refs/got/ namespace. */
1509 fdbea373 2023-07-10 thomas TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1510 fdbea373 2023-07-10 thomas refname = got_ref_get_name(re->ref);
1511 fdbea373 2023-07-10 thomas if (strncmp("refs/got/", refname, 9) != 0)
1512 fdbea373 2023-07-10 thomas continue;
1513 fdbea373 2023-07-10 thomas TAILQ_REMOVE(&include_refs, re, entry);
1514 fdbea373 2023-07-10 thomas got_ref_close(re->ref);
1515 fdbea373 2023-07-10 thomas free(re);
1518 fdbea373 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
1519 fdbea373 2023-07-10 thomas ppa.out = stderr;
1520 fdbea373 2023-07-10 thomas ppa.verbosity = verbosity;
1522 fdbea373 2023-07-10 thomas error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1523 fdbea373 2023-07-10 thomas repo, pack_progress, &ppa, check_cancelled, NULL);
1524 fdbea373 2023-07-10 thomas if (ppa.printed_something)
1525 fdbea373 2023-07-10 thomas fprintf(stderr, "\n");
1527 fdbea373 2023-07-10 thomas if (repo)
1528 fdbea373 2023-07-10 thomas got_repo_close(repo);
1530 fdbea373 2023-07-10 thomas if (pack_fds) {
1531 fdbea373 2023-07-10 thomas const struct got_error *pack_err;
1533 fdbea373 2023-07-10 thomas pack_err = got_repo_pack_fds_close(pack_fds);
1534 7cd52833 2022-06-23 thomas if (error == NULL)
1535 7cd52833 2022-06-23 thomas error = pack_err;
1538 fdbea373 2023-07-10 thomas got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1539 fdbea373 2023-07-10 thomas got_ref_list_free(&exclude_refs);
1540 fdbea373 2023-07-10 thomas got_ref_list_free(&include_refs);
1541 1ea7ccc6 2021-11-15 thomas free(repo_path);
1543 b3d68e7f 2021-07-03 stsp return error;