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 20662ea0 2021-04-10 stsp #include <sys/queue.h>
18 05118f5a 2021-06-22 stsp #include <sys/types.h>
20 05118f5a 2021-06-22 stsp #include <ctype.h>
21 20662ea0 2021-04-10 stsp #include <getopt.h>
22 20662ea0 2021-04-10 stsp #include <err.h>
23 20662ea0 2021-04-10 stsp #include <errno.h>
24 20662ea0 2021-04-10 stsp #include <locale.h>
25 05118f5a 2021-06-22 stsp #include <inttypes.h>
26 05118f5a 2021-06-22 stsp #include <sha1.h>
27 20662ea0 2021-04-10 stsp #include <stdio.h>
28 20662ea0 2021-04-10 stsp #include <stdlib.h>
29 20662ea0 2021-04-10 stsp #include <signal.h>
30 20662ea0 2021-04-10 stsp #include <string.h>
31 20662ea0 2021-04-10 stsp #include <unistd.h>
32 20662ea0 2021-04-10 stsp #include <util.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 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
42 20662ea0 2021-04-10 stsp #include "got_path.h"
43 20662ea0 2021-04-10 stsp #include "got_privsep.h"
44 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
45 7d69d862 2021-11-15 stsp #include "got_worktree.h"
47 20662ea0 2021-04-10 stsp #ifndef nitems
48 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
52 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
55 20662ea0 2021-04-10 stsp catch_sigint(int signo)
57 20662ea0 2021-04-10 stsp sigint_received = 1;
61 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
63 20662ea0 2021-04-10 stsp sigpipe_received = 1;
66 05118f5a 2021-06-22 stsp static const struct got_error *
67 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
69 05118f5a 2021-06-22 stsp if (sigint_received || sigpipe_received)
70 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_CANCELLED);
71 05118f5a 2021-06-22 stsp return NULL;
74 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
75 20662ea0 2021-04-10 stsp const char *cmd_name;
76 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
78 20662ea0 2021-04-10 stsp const char *cmd_alias;
81 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
82 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
83 05118f5a 2021-06-22 stsp __dead static void usage_pack(void);
84 05118f5a 2021-06-22 stsp __dead static void usage_indexpack(void);
85 05118f5a 2021-06-22 stsp __dead static void usage_listpack(void);
86 b3d68e7f 2021-07-03 stsp __dead static void usage_cleanup(void);
88 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
89 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
90 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
91 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
92 b3d68e7f 2021-07-03 stsp static const struct got_error* cmd_cleanup(int, char *[]);
94 3e166534 2022-02-16 naddy static const struct gotadmin_cmd gotadmin_commands[] = {
95 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
96 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
97 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
98 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
99 b3d68e7f 2021-07-03 stsp { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
102 20662ea0 2021-04-10 stsp static void
103 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
107 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
108 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
109 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
110 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
112 20662ea0 2021-04-10 stsp fputc('\n', fp);
116 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
118 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd;
121 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
122 3e166534 2022-02-16 naddy static const struct option longopts[] = {
123 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
124 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
127 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
129 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
130 20662ea0 2021-04-10 stsp switch (ch) {
138 20662ea0 2021-04-10 stsp usage(hflag, 1);
139 20662ea0 2021-04-10 stsp /* NOTREACHED */
143 20662ea0 2021-04-10 stsp argc -= optind;
144 20662ea0 2021-04-10 stsp argv += optind;
145 20662ea0 2021-04-10 stsp optind = 1;
146 20662ea0 2021-04-10 stsp optreset = 1;
148 20662ea0 2021-04-10 stsp if (Vflag) {
149 20662ea0 2021-04-10 stsp got_version_print_str();
153 20662ea0 2021-04-10 stsp if (argc <= 0)
154 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
156 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
157 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
159 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
160 20662ea0 2021-04-10 stsp const struct got_error *error;
162 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
164 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
165 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
169 3e166534 2022-02-16 naddy cmd->cmd_usage();
171 3e166534 2022-02-16 naddy error = cmd->cmd_main(argc, argv);
172 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
173 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
174 20662ea0 2021-04-10 stsp !(sigpipe_received &&
175 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
176 20662ea0 2021-04-10 stsp !(sigint_received &&
177 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
178 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
185 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 20662ea0 2021-04-10 stsp list_commands(stderr);
190 20662ea0 2021-04-10 stsp __dead static void
191 20662ea0 2021-04-10 stsp usage(int hflag, int status)
193 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
195 20662ea0 2021-04-10 stsp fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
196 20662ea0 2021-04-10 stsp getprogname());
198 20662ea0 2021-04-10 stsp list_commands(fp);
199 20662ea0 2021-04-10 stsp exit(status);
202 20662ea0 2021-04-10 stsp static const struct got_error *
203 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
205 20662ea0 2021-04-10 stsp const struct got_error *err;
207 20662ea0 2021-04-10 stsp #ifdef PROFILE
208 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
209 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
211 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
212 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
214 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
215 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
217 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
218 20662ea0 2021-04-10 stsp if (err != NULL)
219 20662ea0 2021-04-10 stsp return err;
221 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
222 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
224 20662ea0 2021-04-10 stsp return NULL;
227 20662ea0 2021-04-10 stsp __dead static void
228 20662ea0 2021-04-10 stsp usage_info(void)
230 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
231 20662ea0 2021-04-10 stsp getprogname());
235 20662ea0 2021-04-10 stsp static const struct got_error *
236 7d69d862 2021-11-15 stsp get_repo_path(char **repo_path)
238 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
239 7d69d862 2021-11-15 stsp struct got_worktree *worktree = NULL;
242 7d69d862 2021-11-15 stsp *repo_path = NULL;
244 7d69d862 2021-11-15 stsp cwd = getcwd(NULL, 0);
245 7d69d862 2021-11-15 stsp if (cwd == NULL)
246 7d69d862 2021-11-15 stsp return got_error_from_errno("getcwd");
248 7d69d862 2021-11-15 stsp err = got_worktree_open(&worktree, cwd);
250 7d69d862 2021-11-15 stsp if (err->code != GOT_ERR_NOT_WORKTREE)
252 7d69d862 2021-11-15 stsp err = NULL;
255 7d69d862 2021-11-15 stsp if (worktree)
256 7d69d862 2021-11-15 stsp *repo_path = strdup(got_worktree_get_repo_path(worktree));
258 7d69d862 2021-11-15 stsp *repo_path = strdup(cwd);
259 7d69d862 2021-11-15 stsp if (*repo_path == NULL)
260 7d69d862 2021-11-15 stsp err = got_error_from_errno("strdup");
262 7d69d862 2021-11-15 stsp if (worktree)
263 7d69d862 2021-11-15 stsp got_worktree_close(worktree);
265 7d69d862 2021-11-15 stsp return err;
268 7d69d862 2021-11-15 stsp static const struct got_error *
269 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
271 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
272 7d69d862 2021-11-15 stsp char *repo_path = NULL;
273 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
274 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
275 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
276 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
277 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
279 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
280 20662ea0 2021-04-10 stsp switch (ch) {
282 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
283 20662ea0 2021-04-10 stsp if (repo_path == NULL)
284 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
286 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
289 20662ea0 2021-04-10 stsp usage_info();
290 20662ea0 2021-04-10 stsp /* NOTREACHED */
294 20662ea0 2021-04-10 stsp argc -= optind;
295 20662ea0 2021-04-10 stsp argv += optind;
297 20662ea0 2021-04-10 stsp #ifndef PROFILE
298 20662ea0 2021-04-10 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
299 20662ea0 2021-04-10 stsp NULL) == -1)
300 20662ea0 2021-04-10 stsp err(1, "pledge");
302 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
303 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
307 7d69d862 2021-11-15 stsp error = got_repo_open(&repo, repo_path, NULL);
311 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
315 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
317 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
318 20662ea0 2021-04-10 stsp if (gotconfig) {
319 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
320 20662ea0 2021-04-10 stsp int i, nremotes;
321 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
322 20662ea0 2021-04-10 stsp printf("default author: %s\n",
323 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
325 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
326 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
327 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
328 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
329 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
330 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
331 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
333 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
334 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
335 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
336 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
341 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
342 20662ea0 2021-04-10 stsp &packsize, repo);
345 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
346 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
347 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
348 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
351 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
352 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
355 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
358 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
359 20662ea0 2021-04-10 stsp if (nobj > 0) {
360 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
361 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
364 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
368 20662ea0 2021-04-10 stsp got_repo_close(repo);
369 7d69d862 2021-11-15 stsp free(repo_path);
370 20662ea0 2021-04-10 stsp return error;
373 05118f5a 2021-06-22 stsp __dead static void
374 05118f5a 2021-06-22 stsp usage_pack(void)
376 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
377 05118f5a 2021-06-22 stsp "[-x reference] [reference ...]\n",
378 05118f5a 2021-06-22 stsp getprogname());
382 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
383 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
384 b8af7c06 2022-03-15 stsp int last_ncolored;
385 b8af7c06 2022-03-15 stsp int last_nfound;
386 b8af7c06 2022-03-15 stsp int last_ntrees;
387 b8af7c06 2022-03-15 stsp int loading_done;
388 05118f5a 2021-06-22 stsp int last_ncommits;
389 05118f5a 2021-06-22 stsp int last_nobj_total;
390 05118f5a 2021-06-22 stsp int last_p_deltify;
391 05118f5a 2021-06-22 stsp int last_p_written;
392 05118f5a 2021-06-22 stsp int last_p_indexed;
393 05118f5a 2021-06-22 stsp int last_p_resolved;
394 05118f5a 2021-06-22 stsp int verbosity;
395 05118f5a 2021-06-22 stsp int printed_something;
398 b8af7c06 2022-03-15 stsp static void
399 b8af7c06 2022-03-15 stsp print_load_info(int print_colored, int print_found, int print_trees,
400 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees)
402 b8af7c06 2022-03-15 stsp if (print_colored) {
403 b8af7c06 2022-03-15 stsp printf("%d commit%s colored", ncolored,
404 b8af7c06 2022-03-15 stsp ncolored == 1 ? "" : "s");
406 b8af7c06 2022-03-15 stsp if (print_found) {
407 b8af7c06 2022-03-15 stsp printf("%s%d object%s found",
408 b8af7c06 2022-03-15 stsp ncolored > 0 ? "; " : "",
409 b8af7c06 2022-03-15 stsp nfound, nfound == 1 ? "" : "s");
411 b8af7c06 2022-03-15 stsp if (print_trees) {
412 b8af7c06 2022-03-15 stsp printf("; %d tree%s scanned", ntrees,
413 b8af7c06 2022-03-15 stsp ntrees == 1 ? "" : "s");
417 05118f5a 2021-06-22 stsp static const struct got_error *
418 b8af7c06 2022-03-15 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
419 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
420 b8af7c06 2022-03-15 stsp int nobj_written)
422 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
423 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
424 05118f5a 2021-06-22 stsp int p_deltify, p_written;
425 b8af7c06 2022-03-15 stsp int print_colored = 0, print_found = 0, print_trees = 0;
426 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
427 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
429 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
430 b8af7c06 2022-03-15 stsp return NULL;
432 b8af7c06 2022-03-15 stsp if (a->last_ncolored != ncolored) {
433 b8af7c06 2022-03-15 stsp print_colored = 1;
434 b8af7c06 2022-03-15 stsp a->last_ncolored = ncolored;
437 b8af7c06 2022-03-15 stsp if (a->last_nfound != nfound) {
438 b8af7c06 2022-03-15 stsp print_colored = 1;
439 b8af7c06 2022-03-15 stsp print_found = 1;
440 b8af7c06 2022-03-15 stsp a->last_nfound = nfound;
443 b8af7c06 2022-03-15 stsp if (a->last_ntrees != ntrees) {
444 b8af7c06 2022-03-15 stsp print_colored = 1;
445 b8af7c06 2022-03-15 stsp print_found = 1;
446 b8af7c06 2022-03-15 stsp print_trees = 1;
447 b8af7c06 2022-03-15 stsp a->last_ntrees = ntrees;
450 b8af7c06 2022-03-15 stsp if ((print_colored || print_found || print_trees) &&
451 b8af7c06 2022-03-15 stsp !a->loading_done) {
452 b8af7c06 2022-03-15 stsp printf("\r");
453 b8af7c06 2022-03-15 stsp print_load_info(print_colored, print_found, print_trees,
454 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees);
455 b8af7c06 2022-03-15 stsp a->printed_something = 1;
456 b8af7c06 2022-03-15 stsp fflush(stdout);
457 05118f5a 2021-06-22 stsp return NULL;
458 b8af7c06 2022-03-15 stsp } else if (!a->loading_done) {
459 b8af7c06 2022-03-15 stsp printf("\r");
460 b8af7c06 2022-03-15 stsp print_load_info(1, 1, 1, ncolored, nfound, ntrees);
461 b8af7c06 2022-03-15 stsp printf("\n");
462 b8af7c06 2022-03-15 stsp a->loading_done = 1;
465 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
466 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
468 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
469 05118f5a 2021-06-22 stsp print_searching = 1;
470 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
473 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
474 05118f5a 2021-06-22 stsp print_searching = 1;
475 05118f5a 2021-06-22 stsp print_total = 1;
476 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
479 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
480 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
481 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
482 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
483 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
486 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
487 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
488 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
489 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
490 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
491 05118f5a 2021-06-22 stsp print_searching = 1;
492 05118f5a 2021-06-22 stsp print_total = 1;
493 05118f5a 2021-06-22 stsp print_deltify = 1;
496 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
497 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
498 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
499 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
500 05118f5a 2021-06-22 stsp print_searching = 1;
501 05118f5a 2021-06-22 stsp print_total = 1;
502 05118f5a 2021-06-22 stsp print_deltify = 1;
503 05118f5a 2021-06-22 stsp print_written = 1;
508 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
509 05118f5a 2021-06-22 stsp printf("\r");
510 05118f5a 2021-06-22 stsp if (print_searching)
511 05118f5a 2021-06-22 stsp printf("packing %d reference%s", ncommits,
512 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
513 05118f5a 2021-06-22 stsp if (print_total)
514 05118f5a 2021-06-22 stsp printf("; %d object%s", nobj_total,
515 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
516 05118f5a 2021-06-22 stsp if (print_deltify)
517 05118f5a 2021-06-22 stsp printf("; deltify: %d%%", p_deltify);
518 05118f5a 2021-06-22 stsp if (print_written)
519 b5934965 2022-02-12 naddy printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
520 05118f5a 2021-06-22 stsp scaled_size, p_written);
521 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
522 05118f5a 2021-06-22 stsp print_written) {
523 05118f5a 2021-06-22 stsp a->printed_something = 1;
524 05118f5a 2021-06-22 stsp fflush(stdout);
526 05118f5a 2021-06-22 stsp return NULL;
529 05118f5a 2021-06-22 stsp static const struct got_error *
530 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
531 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
533 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
534 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
535 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
536 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
538 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
539 05118f5a 2021-06-22 stsp return NULL;
541 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
542 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
543 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
544 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
545 05118f5a 2021-06-22 stsp print_size = 1;
546 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
547 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
548 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
550 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
551 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
552 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
553 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
554 05118f5a 2021-06-22 stsp print_indexed = 1;
555 05118f5a 2021-06-22 stsp print_size = 1;
558 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
559 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
560 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
561 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
562 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
563 05118f5a 2021-06-22 stsp print_resolved = 1;
564 05118f5a 2021-06-22 stsp print_indexed = 1;
565 05118f5a 2021-06-22 stsp print_size = 1;
570 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
571 05118f5a 2021-06-22 stsp printf("\r");
572 05118f5a 2021-06-22 stsp if (print_size)
573 b5934965 2022-02-12 naddy printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
574 05118f5a 2021-06-22 stsp if (print_indexed)
575 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
576 05118f5a 2021-06-22 stsp if (print_resolved)
577 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
578 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
579 05118f5a 2021-06-22 stsp fflush(stdout);
581 05118f5a 2021-06-22 stsp return NULL;
584 05118f5a 2021-06-22 stsp static const struct got_error *
585 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
586 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
588 05118f5a 2021-06-22 stsp const struct got_error *err;
589 05118f5a 2021-06-22 stsp struct got_reference *ref;
591 05118f5a 2021-06-22 stsp *new = NULL;
593 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
595 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
596 05118f5a 2021-06-22 stsp return err;
598 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
599 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
600 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
602 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
603 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
604 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
605 05118f5a 2021-06-22 stsp got_ref_close(ref);
608 05118f5a 2021-06-22 stsp return err;
611 05118f5a 2021-06-22 stsp static const struct got_error *
612 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
614 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
615 7d69d862 2021-11-15 stsp char *repo_path = NULL;
616 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
617 05118f5a 2021-06-22 stsp int ch, i, loose_obj_only = 1;
618 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
619 05118f5a 2021-06-22 stsp char *id_str = NULL;
620 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
621 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
622 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
623 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
624 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
625 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
626 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
628 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
629 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
630 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
632 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
633 05118f5a 2021-06-22 stsp switch (ch) {
635 05118f5a 2021-06-22 stsp loose_obj_only = 0;
638 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
639 05118f5a 2021-06-22 stsp if (repo_path == NULL)
640 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
642 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
645 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
646 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
647 05118f5a 2021-06-22 stsp optarg, NULL);
649 05118f5a 2021-06-22 stsp return error;
652 05118f5a 2021-06-22 stsp usage_pack();
653 05118f5a 2021-06-22 stsp /* NOTREACHED */
657 05118f5a 2021-06-22 stsp argc -= optind;
658 05118f5a 2021-06-22 stsp argv += optind;
660 05118f5a 2021-06-22 stsp #ifndef PROFILE
661 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
662 05118f5a 2021-06-22 stsp NULL) == -1)
663 05118f5a 2021-06-22 stsp err(1, "pledge");
665 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
666 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
670 7d69d862 2021-11-15 stsp error = got_repo_open(&repo, repo_path, NULL);
674 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
678 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
679 05118f5a 2021-06-22 stsp const char *refname = pe->path;
680 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
686 05118f5a 2021-06-22 stsp if (argc == 0) {
687 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
688 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
692 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
693 05118f5a 2021-06-22 stsp const char *refname;
694 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
695 05118f5a 2021-06-22 stsp refname = argv[i];
696 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
702 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
703 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
704 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
705 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
707 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
708 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
712 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
713 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
714 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
715 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
717 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
718 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
719 05118f5a 2021-06-22 stsp pack_progress, &ppa, check_cancelled, NULL);
720 05118f5a 2021-06-22 stsp if (error) {
721 05118f5a 2021-06-22 stsp if (ppa.printed_something)
722 05118f5a 2021-06-22 stsp printf("\n");
726 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
729 05118f5a 2021-06-22 stsp printf("\nWrote %s.pack\n", id_str);
731 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
732 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
735 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
738 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
739 05118f5a 2021-06-22 stsp got_pathlist_free(&exclude_args);
740 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
741 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
742 05118f5a 2021-06-22 stsp free(id_str);
743 05118f5a 2021-06-22 stsp free(pack_hash);
744 7d69d862 2021-11-15 stsp free(repo_path);
745 05118f5a 2021-06-22 stsp return error;
748 05118f5a 2021-06-22 stsp __dead static void
749 05118f5a 2021-06-22 stsp usage_indexpack(void)
751 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
752 05118f5a 2021-06-22 stsp getprogname());
756 05118f5a 2021-06-22 stsp static const struct got_error *
757 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
759 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
760 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
762 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
763 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
764 05118f5a 2021-06-22 stsp char *id_str = NULL;
765 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
766 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
768 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
769 05118f5a 2021-06-22 stsp switch (ch) {
771 05118f5a 2021-06-22 stsp usage_indexpack();
772 05118f5a 2021-06-22 stsp /* NOTREACHED */
776 05118f5a 2021-06-22 stsp argc -= optind;
777 05118f5a 2021-06-22 stsp argv += optind;
779 05118f5a 2021-06-22 stsp if (argc != 1)
780 05118f5a 2021-06-22 stsp usage_indexpack();
782 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
783 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
784 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
786 05118f5a 2021-06-22 stsp #ifndef PROFILE
787 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
788 05118f5a 2021-06-22 stsp NULL) == -1)
789 05118f5a 2021-06-22 stsp err(1, "pledge");
792 05118f5a 2021-06-22 stsp error = got_repo_open(&repo, packfile_path, NULL);
796 802c0f04 2021-10-15 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
800 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
801 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
802 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
803 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
805 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
806 05118f5a 2021-06-22 stsp packfile_path);
810 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
814 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
815 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
818 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
821 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
822 05118f5a 2021-06-22 stsp free(id_str);
823 05118f5a 2021-06-22 stsp free(pack_hash);
824 05118f5a 2021-06-22 stsp return error;
827 05118f5a 2021-06-22 stsp __dead static void
828 05118f5a 2021-06-22 stsp usage_listpack(void)
830 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
831 05118f5a 2021-06-22 stsp getprogname());
835 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
836 05118f5a 2021-06-22 stsp int nblobs;
837 05118f5a 2021-06-22 stsp int ntrees;
838 05118f5a 2021-06-22 stsp int ncommits;
840 05118f5a 2021-06-22 stsp int noffdeltas;
841 05118f5a 2021-06-22 stsp int nrefdeltas;
842 05118f5a 2021-06-22 stsp int human_readable;
845 05118f5a 2021-06-22 stsp static const struct got_error *
846 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
847 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
849 05118f5a 2021-06-22 stsp const struct got_error *err;
850 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
851 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
852 05118f5a 2021-06-22 stsp const char *type_str;
854 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, id);
856 05118f5a 2021-06-22 stsp return err;
858 05118f5a 2021-06-22 stsp switch (type) {
859 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
860 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
861 05118f5a 2021-06-22 stsp a->nblobs++;
863 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
864 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
865 05118f5a 2021-06-22 stsp a->ntrees++;
867 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
868 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
869 05118f5a 2021-06-22 stsp a->ncommits++;
871 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
872 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
873 05118f5a 2021-06-22 stsp a->ntags++;
875 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
876 05118f5a 2021-06-22 stsp type_str = "offset-delta";
877 963ac08a 2021-09-25 naddy if (asprintf(&delta_str, " base-offset %lld",
878 963ac08a 2021-09-25 naddy (long long)base_offset) == -1) {
879 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
882 05118f5a 2021-06-22 stsp a->noffdeltas++;
884 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
885 05118f5a 2021-06-22 stsp type_str = "ref-delta";
886 05118f5a 2021-06-22 stsp err = got_object_id_str(&base_id_str, base_id);
889 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
890 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
893 05118f5a 2021-06-22 stsp a->nrefdeltas++;
896 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
899 05118f5a 2021-06-22 stsp if (a->human_readable) {
900 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
902 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
903 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
906 05118f5a 2021-06-22 stsp s = scaled;
907 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
909 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %s%s\n", id_str, type_str,
910 963ac08a 2021-09-25 naddy (long long)offset, s, delta_str ? delta_str : "");
912 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %lld%s\n", id_str, type_str,
913 963ac08a 2021-09-25 naddy (long long)offset, (long long)size,
914 963ac08a 2021-09-25 naddy delta_str ? delta_str : "");
917 05118f5a 2021-06-22 stsp free(id_str);
918 05118f5a 2021-06-22 stsp free(base_id_str);
919 05118f5a 2021-06-22 stsp free(delta_str);
920 05118f5a 2021-06-22 stsp return err;
923 05118f5a 2021-06-22 stsp static const struct got_error *
924 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
926 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
927 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
929 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
930 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
931 05118f5a 2021-06-22 stsp char *id_str = NULL;
932 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
933 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
934 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
936 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
937 05118f5a 2021-06-22 stsp switch (ch) {
939 05118f5a 2021-06-22 stsp human_readable = 1;
942 05118f5a 2021-06-22 stsp show_stats = 1;
945 05118f5a 2021-06-22 stsp usage_listpack();
946 05118f5a 2021-06-22 stsp /* NOTREACHED */
950 05118f5a 2021-06-22 stsp argc -= optind;
951 05118f5a 2021-06-22 stsp argv += optind;
953 05118f5a 2021-06-22 stsp if (argc != 1)
954 05118f5a 2021-06-22 stsp usage_listpack();
955 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
956 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
957 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
959 05118f5a 2021-06-22 stsp #ifndef PROFILE
960 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
961 05118f5a 2021-06-22 stsp NULL) == -1)
962 05118f5a 2021-06-22 stsp err(1, "pledge");
964 05118f5a 2021-06-22 stsp error = got_repo_open(&repo, packfile_path, NULL);
968 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
972 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
973 05118f5a 2021-06-22 stsp packfile_path);
976 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
980 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
981 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
982 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
983 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
986 05118f5a 2021-06-22 stsp if (show_stats) {
987 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
988 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
989 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
990 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
991 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
992 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
996 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
997 05118f5a 2021-06-22 stsp free(id_str);
998 05118f5a 2021-06-22 stsp free(pack_hash);
999 05118f5a 2021-06-22 stsp free(packfile_path);
1000 05118f5a 2021-06-22 stsp return error;
1003 b3d68e7f 2021-07-03 stsp __dead static void
1004 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1006 ef8ec606 2021-07-27 stsp fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1007 1124fe40 2021-07-07 stsp "[-q]\n", getprogname());
1011 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1012 b3d68e7f 2021-07-03 stsp int last_nloose;
1013 b3d68e7f 2021-07-03 stsp int last_ncommits;
1014 b3d68e7f 2021-07-03 stsp int last_npurged;
1015 b3d68e7f 2021-07-03 stsp int verbosity;
1016 b3d68e7f 2021-07-03 stsp int printed_something;
1017 b3d68e7f 2021-07-03 stsp int dry_run;
1020 b3d68e7f 2021-07-03 stsp static const struct got_error *
1021 b3d68e7f 2021-07-03 stsp cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1023 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1024 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1026 b3d68e7f 2021-07-03 stsp if (a->last_nloose != nloose) {
1027 b3d68e7f 2021-07-03 stsp print_loose = 1;
1028 b3d68e7f 2021-07-03 stsp a->last_nloose = nloose;
1030 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1031 b3d68e7f 2021-07-03 stsp print_loose = 1;
1032 b3d68e7f 2021-07-03 stsp print_commits = 1;
1033 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1035 b3d68e7f 2021-07-03 stsp if (a->last_npurged != npurged) {
1036 b3d68e7f 2021-07-03 stsp print_loose = 1;
1037 b3d68e7f 2021-07-03 stsp print_commits = 1;
1038 b3d68e7f 2021-07-03 stsp print_purged = 1;
1039 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1042 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1043 b3d68e7f 2021-07-03 stsp return NULL;
1045 b3d68e7f 2021-07-03 stsp if (print_loose || print_commits || print_purged)
1046 b3d68e7f 2021-07-03 stsp printf("\r");
1047 b3d68e7f 2021-07-03 stsp if (print_loose)
1048 b3d68e7f 2021-07-03 stsp printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1049 b3d68e7f 2021-07-03 stsp if (print_commits)
1050 b3d68e7f 2021-07-03 stsp printf("; %d commit%s scanned", ncommits,
1051 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1052 b3d68e7f 2021-07-03 stsp if (print_purged) {
1053 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1054 b3d68e7f 2021-07-03 stsp printf("; %d object%s could be purged", npurged,
1055 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1057 b3d68e7f 2021-07-03 stsp printf("; %d object%s purged", npurged,
1058 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1061 b3d68e7f 2021-07-03 stsp if (print_loose || print_commits || print_purged) {
1062 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1063 b3d68e7f 2021-07-03 stsp fflush(stdout);
1065 b3d68e7f 2021-07-03 stsp return NULL;
1068 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1069 1124fe40 2021-07-07 stsp int verbosity;
1070 1124fe40 2021-07-07 stsp int printed_something;
1071 1124fe40 2021-07-07 stsp int dry_run;
1074 b3d68e7f 2021-07-03 stsp static const struct got_error *
1075 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1077 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1079 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1080 1124fe40 2021-07-07 stsp return NULL;
1082 1124fe40 2021-07-07 stsp if (a->dry_run)
1083 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1085 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1087 1124fe40 2021-07-07 stsp a->printed_something = 1;
1088 1124fe40 2021-07-07 stsp return NULL;
1091 1124fe40 2021-07-07 stsp static const struct got_error *
1092 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1094 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1095 7d69d862 2021-11-15 stsp char *repo_path = NULL;
1096 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1097 b3d68e7f 2021-07-03 stsp int ch, dry_run = 0, npacked = 0, verbosity = 0;
1098 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1099 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1100 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1101 b3d68e7f 2021-07-03 stsp off_t size_before, size_after;
1102 b3d68e7f 2021-07-03 stsp char scaled_before[FMT_SCALED_STRSIZE];
1103 b3d68e7f 2021-07-03 stsp char scaled_after[FMT_SCALED_STRSIZE];
1104 b3d68e7f 2021-07-03 stsp char scaled_diff[FMT_SCALED_STRSIZE];
1105 9188bd78 2021-07-03 stsp char **extensions;
1106 9188bd78 2021-07-03 stsp int nextensions, i;
1108 ef8ec606 2021-07-27 stsp while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1109 b3d68e7f 2021-07-03 stsp switch (ch) {
1111 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1114 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1117 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1118 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1119 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1121 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1124 b3d68e7f 2021-07-03 stsp dry_run = 1;
1127 b3d68e7f 2021-07-03 stsp verbosity = -1;
1130 b3d68e7f 2021-07-03 stsp usage_cleanup();
1131 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1135 b3d68e7f 2021-07-03 stsp argc -= optind;
1136 b3d68e7f 2021-07-03 stsp argv += optind;
1138 b3d68e7f 2021-07-03 stsp #ifndef PROFILE
1139 b3d68e7f 2021-07-03 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1140 b3d68e7f 2021-07-03 stsp NULL) == -1)
1141 b3d68e7f 2021-07-03 stsp err(1, "pledge");
1143 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
1144 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
1145 7d69d862 2021-11-15 stsp if (error)
1146 7d69d862 2021-11-15 stsp goto done;
1148 7d69d862 2021-11-15 stsp error = got_repo_open(&repo, repo_path, NULL);
1149 b3d68e7f 2021-07-03 stsp if (error)
1150 b3d68e7f 2021-07-03 stsp goto done;
1152 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1153 b3d68e7f 2021-07-03 stsp if (error)
1154 b3d68e7f 2021-07-03 stsp goto done;
1156 9188bd78 2021-07-03 stsp got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1158 9188bd78 2021-07-03 stsp for (i = 0; i < nextensions; i++) {
1159 9188bd78 2021-07-03 stsp if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1160 9188bd78 2021-07-03 stsp error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1161 9188bd78 2021-07-03 stsp "the preciousObjects Git extension is enabled; "
1162 9188bd78 2021-07-03 stsp "this implies that objects must not be deleted");
1163 9188bd78 2021-07-03 stsp goto done;
1167 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1168 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1169 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1170 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1171 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1172 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1173 1124fe40 2021-07-07 stsp goto done;
1176 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1177 b3d68e7f 2021-07-03 stsp cpa.last_ncommits = -1;
1178 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1179 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1180 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1181 b3d68e7f 2021-07-03 stsp error = got_repo_purge_unreferenced_loose_objects(repo,
1182 abc59930 2021-09-05 naddy &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1183 abc59930 2021-09-05 naddy cleanup_progress, &cpa, check_cancelled, NULL);
1184 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1185 b3d68e7f 2021-07-03 stsp printf("\n");
1186 b3d68e7f 2021-07-03 stsp if (error)
1187 b3d68e7f 2021-07-03 stsp goto done;
1188 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1189 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_before, scaled_before) == -1) {
1190 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1191 b3d68e7f 2021-07-03 stsp goto done;
1193 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_after, scaled_after) == -1) {
1194 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1195 b3d68e7f 2021-07-03 stsp goto done;
1197 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1198 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1199 b3d68e7f 2021-07-03 stsp goto done;
1201 b3d68e7f 2021-07-03 stsp printf("loose total size before: %s\n", scaled_before);
1202 b3d68e7f 2021-07-03 stsp printf("loose total size after: %s\n", scaled_after);
1203 b3d68e7f 2021-07-03 stsp if (dry_run) {
1204 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1205 b3d68e7f 2021-07-03 stsp scaled_diff);
1207 b3d68e7f 2021-07-03 stsp printf("disk space freed: %s\n", scaled_diff);
1208 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1212 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1213 7d69d862 2021-11-15 stsp free(repo_path);
1214 b3d68e7f 2021-07-03 stsp return error;