Blame


1 20662ea0 2021-04-10 stsp /*
2 20662ea0 2021-04-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 20662ea0 2021-04-10 stsp *
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.
7 20662ea0 2021-04-10 stsp *
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.
15 20662ea0 2021-04-10 stsp */
16 20662ea0 2021-04-10 stsp
17 20662ea0 2021-04-10 stsp #include <sys/queue.h>
18 05118f5a 2021-06-22 stsp #include <sys/types.h>
19 20662ea0 2021-04-10 stsp
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>
33 20662ea0 2021-04-10 stsp
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"
46 20662ea0 2021-04-10 stsp
47 20662ea0 2021-04-10 stsp #ifndef nitems
48 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 20662ea0 2021-04-10 stsp #endif
50 20662ea0 2021-04-10 stsp
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;
53 20662ea0 2021-04-10 stsp
54 20662ea0 2021-04-10 stsp static void
55 20662ea0 2021-04-10 stsp catch_sigint(int signo)
56 20662ea0 2021-04-10 stsp {
57 20662ea0 2021-04-10 stsp sigint_received = 1;
58 20662ea0 2021-04-10 stsp }
59 20662ea0 2021-04-10 stsp
60 20662ea0 2021-04-10 stsp static void
61 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
62 20662ea0 2021-04-10 stsp {
63 20662ea0 2021-04-10 stsp sigpipe_received = 1;
64 20662ea0 2021-04-10 stsp }
65 20662ea0 2021-04-10 stsp
66 05118f5a 2021-06-22 stsp static const struct got_error *
67 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
68 05118f5a 2021-06-22 stsp {
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;
72 05118f5a 2021-06-22 stsp }
73 20662ea0 2021-04-10 stsp
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;
79 20662ea0 2021-04-10 stsp };
80 20662ea0 2021-04-10 stsp
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);
87 20662ea0 2021-04-10 stsp
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 *[]);
93 20662ea0 2021-04-10 stsp
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" },
100 20662ea0 2021-04-10 stsp };
101 20662ea0 2021-04-10 stsp
102 20662ea0 2021-04-10 stsp static void
103 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
104 20662ea0 2021-04-10 stsp {
105 20662ea0 2021-04-10 stsp size_t i;
106 20662ea0 2021-04-10 stsp
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);
111 20662ea0 2021-04-10 stsp }
112 20662ea0 2021-04-10 stsp fputc('\n', fp);
113 20662ea0 2021-04-10 stsp }
114 20662ea0 2021-04-10 stsp
115 20662ea0 2021-04-10 stsp int
116 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
117 20662ea0 2021-04-10 stsp {
118 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd;
119 20662ea0 2021-04-10 stsp size_t i;
120 20662ea0 2021-04-10 stsp int ch;
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 }
125 20662ea0 2021-04-10 stsp };
126 20662ea0 2021-04-10 stsp
127 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
128 20662ea0 2021-04-10 stsp
129 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
130 20662ea0 2021-04-10 stsp switch (ch) {
131 20662ea0 2021-04-10 stsp case 'h':
132 20662ea0 2021-04-10 stsp hflag = 1;
133 20662ea0 2021-04-10 stsp break;
134 20662ea0 2021-04-10 stsp case 'V':
135 20662ea0 2021-04-10 stsp Vflag = 1;
136 20662ea0 2021-04-10 stsp break;
137 20662ea0 2021-04-10 stsp default:
138 20662ea0 2021-04-10 stsp usage(hflag, 1);
139 20662ea0 2021-04-10 stsp /* NOTREACHED */
140 20662ea0 2021-04-10 stsp }
141 20662ea0 2021-04-10 stsp }
142 20662ea0 2021-04-10 stsp
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;
147 20662ea0 2021-04-10 stsp
148 20662ea0 2021-04-10 stsp if (Vflag) {
149 20662ea0 2021-04-10 stsp got_version_print_str();
150 20662ea0 2021-04-10 stsp return 0;
151 20662ea0 2021-04-10 stsp }
152 20662ea0 2021-04-10 stsp
153 20662ea0 2021-04-10 stsp if (argc <= 0)
154 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
155 20662ea0 2021-04-10 stsp
156 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
157 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
158 20662ea0 2021-04-10 stsp
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;
161 20662ea0 2021-04-10 stsp
162 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
163 20662ea0 2021-04-10 stsp
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)
166 20662ea0 2021-04-10 stsp continue;
167 20662ea0 2021-04-10 stsp
168 20662ea0 2021-04-10 stsp if (hflag)
169 3e166534 2022-02-16 naddy cmd->cmd_usage();
170 20662ea0 2021-04-10 stsp
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);
179 20662ea0 2021-04-10 stsp return 1;
180 20662ea0 2021-04-10 stsp }
181 20662ea0 2021-04-10 stsp
182 20662ea0 2021-04-10 stsp return 0;
183 20662ea0 2021-04-10 stsp }
184 20662ea0 2021-04-10 stsp
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);
187 20662ea0 2021-04-10 stsp return 1;
188 20662ea0 2021-04-10 stsp }
189 20662ea0 2021-04-10 stsp
190 20662ea0 2021-04-10 stsp __dead static void
191 20662ea0 2021-04-10 stsp usage(int hflag, int status)
192 20662ea0 2021-04-10 stsp {
193 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
194 20662ea0 2021-04-10 stsp
195 20662ea0 2021-04-10 stsp fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
196 20662ea0 2021-04-10 stsp getprogname());
197 20662ea0 2021-04-10 stsp if (hflag)
198 20662ea0 2021-04-10 stsp list_commands(fp);
199 20662ea0 2021-04-10 stsp exit(status);
200 20662ea0 2021-04-10 stsp }
201 20662ea0 2021-04-10 stsp
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)
204 20662ea0 2021-04-10 stsp {
205 20662ea0 2021-04-10 stsp const struct got_error *err;
206 20662ea0 2021-04-10 stsp
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");
210 20662ea0 2021-04-10 stsp #endif
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);
213 20662ea0 2021-04-10 stsp
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);
216 20662ea0 2021-04-10 stsp
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;
220 20662ea0 2021-04-10 stsp
221 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
222 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
223 20662ea0 2021-04-10 stsp
224 20662ea0 2021-04-10 stsp return NULL;
225 20662ea0 2021-04-10 stsp }
226 20662ea0 2021-04-10 stsp
227 20662ea0 2021-04-10 stsp __dead static void
228 20662ea0 2021-04-10 stsp usage_info(void)
229 20662ea0 2021-04-10 stsp {
230 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
231 20662ea0 2021-04-10 stsp getprogname());
232 20662ea0 2021-04-10 stsp exit(1);
233 20662ea0 2021-04-10 stsp }
234 20662ea0 2021-04-10 stsp
235 20662ea0 2021-04-10 stsp static const struct got_error *
236 7d69d862 2021-11-15 stsp get_repo_path(char **repo_path)
237 7d69d862 2021-11-15 stsp {
238 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
239 7d69d862 2021-11-15 stsp struct got_worktree *worktree = NULL;
240 7d69d862 2021-11-15 stsp char *cwd;
241 7d69d862 2021-11-15 stsp
242 7d69d862 2021-11-15 stsp *repo_path = NULL;
243 7d69d862 2021-11-15 stsp
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");
247 7d69d862 2021-11-15 stsp
248 7d69d862 2021-11-15 stsp err = got_worktree_open(&worktree, cwd);
249 7d69d862 2021-11-15 stsp if (err) {
250 7d69d862 2021-11-15 stsp if (err->code != GOT_ERR_NOT_WORKTREE)
251 7d69d862 2021-11-15 stsp goto done;
252 7d69d862 2021-11-15 stsp err = NULL;
253 7d69d862 2021-11-15 stsp }
254 7d69d862 2021-11-15 stsp
255 7d69d862 2021-11-15 stsp if (worktree)
256 7d69d862 2021-11-15 stsp *repo_path = strdup(got_worktree_get_repo_path(worktree));
257 7d69d862 2021-11-15 stsp else
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");
261 7d69d862 2021-11-15 stsp done:
262 7d69d862 2021-11-15 stsp if (worktree)
263 7d69d862 2021-11-15 stsp got_worktree_close(worktree);
264 7d69d862 2021-11-15 stsp free(cwd);
265 7d69d862 2021-11-15 stsp return err;
266 7d69d862 2021-11-15 stsp }
267 7d69d862 2021-11-15 stsp
268 7d69d862 2021-11-15 stsp static const struct got_error *
269 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
270 20662ea0 2021-04-10 stsp {
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];
278 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
279 20662ea0 2021-04-10 stsp
280 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
281 20662ea0 2021-04-10 stsp switch (ch) {
282 20662ea0 2021-04-10 stsp case 'r':
283 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
284 20662ea0 2021-04-10 stsp if (repo_path == NULL)
285 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
286 20662ea0 2021-04-10 stsp optarg);
287 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
288 20662ea0 2021-04-10 stsp break;
289 20662ea0 2021-04-10 stsp default:
290 20662ea0 2021-04-10 stsp usage_info();
291 20662ea0 2021-04-10 stsp /* NOTREACHED */
292 20662ea0 2021-04-10 stsp }
293 20662ea0 2021-04-10 stsp }
294 20662ea0 2021-04-10 stsp
295 20662ea0 2021-04-10 stsp argc -= optind;
296 20662ea0 2021-04-10 stsp argv += optind;
297 20662ea0 2021-04-10 stsp
298 20662ea0 2021-04-10 stsp #ifndef PROFILE
299 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
300 20662ea0 2021-04-10 stsp NULL) == -1)
301 20662ea0 2021-04-10 stsp err(1, "pledge");
302 20662ea0 2021-04-10 stsp #endif
303 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
304 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
305 7d69d862 2021-11-15 stsp if (error)
306 7d69d862 2021-11-15 stsp goto done;
307 20662ea0 2021-04-10 stsp }
308 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
309 0ae84acc 2022-06-15 tracey if (error != NULL)
310 0ae84acc 2022-06-15 tracey goto done;
311 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
312 20662ea0 2021-04-10 stsp if (error)
313 20662ea0 2021-04-10 stsp goto done;
314 57160834 2022-05-31 stsp #ifndef PROFILE
315 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
316 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
317 57160834 2022-05-31 stsp NULL) == -1)
318 57160834 2022-05-31 stsp err(1, "pledge");
319 57160834 2022-05-31 stsp #endif
320 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
321 20662ea0 2021-04-10 stsp if (error)
322 20662ea0 2021-04-10 stsp goto done;
323 20662ea0 2021-04-10 stsp
324 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
325 20662ea0 2021-04-10 stsp
326 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
327 20662ea0 2021-04-10 stsp if (gotconfig) {
328 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
329 20662ea0 2021-04-10 stsp int i, nremotes;
330 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
331 20662ea0 2021-04-10 stsp printf("default author: %s\n",
332 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
333 20662ea0 2021-04-10 stsp }
334 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
335 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
336 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
337 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
338 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
339 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
340 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
341 13b2084e 2021-09-06 stsp } else {
342 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
343 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
344 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
345 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
346 13b2084e 2021-09-06 stsp }
347 20662ea0 2021-04-10 stsp }
348 20662ea0 2021-04-10 stsp }
349 20662ea0 2021-04-10 stsp
350 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
351 20662ea0 2021-04-10 stsp &packsize, repo);
352 20662ea0 2021-04-10 stsp if (error)
353 20662ea0 2021-04-10 stsp goto done;
354 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
355 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
356 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
357 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
358 20662ea0 2021-04-10 stsp goto done;
359 20662ea0 2021-04-10 stsp }
360 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
361 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
362 20662ea0 2021-04-10 stsp }
363 20662ea0 2021-04-10 stsp
364 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
365 20662ea0 2021-04-10 stsp if (error)
366 20662ea0 2021-04-10 stsp goto done;
367 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
368 20662ea0 2021-04-10 stsp if (nobj > 0) {
369 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
370 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
371 20662ea0 2021-04-10 stsp goto done;
372 20662ea0 2021-04-10 stsp }
373 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
374 20662ea0 2021-04-10 stsp }
375 20662ea0 2021-04-10 stsp done:
376 20662ea0 2021-04-10 stsp if (repo)
377 20662ea0 2021-04-10 stsp got_repo_close(repo);
378 0ae84acc 2022-06-15 tracey if (pack_fds) {
379 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
380 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
381 0ae84acc 2022-06-15 tracey if (error == NULL)
382 0ae84acc 2022-06-15 tracey error = pack_err;
383 0ae84acc 2022-06-15 tracey }
384 0ae84acc 2022-06-15 tracey
385 7d69d862 2021-11-15 stsp free(repo_path);
386 20662ea0 2021-04-10 stsp return error;
387 05118f5a 2021-06-22 stsp }
388 05118f5a 2021-06-22 stsp
389 05118f5a 2021-06-22 stsp __dead static void
390 05118f5a 2021-06-22 stsp usage_pack(void)
391 05118f5a 2021-06-22 stsp {
392 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
393 45ca4633 2022-04-23 naddy "[-x reference] [-q] [reference ...]\n",
394 05118f5a 2021-06-22 stsp getprogname());
395 05118f5a 2021-06-22 stsp exit(1);
396 05118f5a 2021-06-22 stsp }
397 05118f5a 2021-06-22 stsp
398 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
399 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
400 b8af7c06 2022-03-15 stsp int last_ncolored;
401 b8af7c06 2022-03-15 stsp int last_nfound;
402 b8af7c06 2022-03-15 stsp int last_ntrees;
403 b8af7c06 2022-03-15 stsp int loading_done;
404 05118f5a 2021-06-22 stsp int last_ncommits;
405 05118f5a 2021-06-22 stsp int last_nobj_total;
406 05118f5a 2021-06-22 stsp int last_p_deltify;
407 05118f5a 2021-06-22 stsp int last_p_written;
408 05118f5a 2021-06-22 stsp int last_p_indexed;
409 05118f5a 2021-06-22 stsp int last_p_resolved;
410 05118f5a 2021-06-22 stsp int verbosity;
411 05118f5a 2021-06-22 stsp int printed_something;
412 05118f5a 2021-06-22 stsp };
413 05118f5a 2021-06-22 stsp
414 b8af7c06 2022-03-15 stsp static void
415 b8af7c06 2022-03-15 stsp print_load_info(int print_colored, int print_found, int print_trees,
416 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees)
417 b8af7c06 2022-03-15 stsp {
418 b8af7c06 2022-03-15 stsp if (print_colored) {
419 b8af7c06 2022-03-15 stsp printf("%d commit%s colored", ncolored,
420 b8af7c06 2022-03-15 stsp ncolored == 1 ? "" : "s");
421 b8af7c06 2022-03-15 stsp }
422 b8af7c06 2022-03-15 stsp if (print_found) {
423 b8af7c06 2022-03-15 stsp printf("%s%d object%s found",
424 b8af7c06 2022-03-15 stsp ncolored > 0 ? "; " : "",
425 b8af7c06 2022-03-15 stsp nfound, nfound == 1 ? "" : "s");
426 b8af7c06 2022-03-15 stsp }
427 b8af7c06 2022-03-15 stsp if (print_trees) {
428 b8af7c06 2022-03-15 stsp printf("; %d tree%s scanned", ntrees,
429 b8af7c06 2022-03-15 stsp ntrees == 1 ? "" : "s");
430 b8af7c06 2022-03-15 stsp }
431 b8af7c06 2022-03-15 stsp }
432 b8af7c06 2022-03-15 stsp
433 05118f5a 2021-06-22 stsp static const struct got_error *
434 b8af7c06 2022-03-15 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
435 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
436 b8af7c06 2022-03-15 stsp int nobj_written)
437 05118f5a 2021-06-22 stsp {
438 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
439 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
440 05118f5a 2021-06-22 stsp int p_deltify, p_written;
441 b8af7c06 2022-03-15 stsp int print_colored = 0, print_found = 0, print_trees = 0;
442 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
443 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
444 05118f5a 2021-06-22 stsp
445 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
446 b8af7c06 2022-03-15 stsp return NULL;
447 b8af7c06 2022-03-15 stsp
448 b8af7c06 2022-03-15 stsp if (a->last_ncolored != ncolored) {
449 b8af7c06 2022-03-15 stsp print_colored = 1;
450 b8af7c06 2022-03-15 stsp a->last_ncolored = ncolored;
451 b8af7c06 2022-03-15 stsp }
452 b8af7c06 2022-03-15 stsp
453 b8af7c06 2022-03-15 stsp if (a->last_nfound != nfound) {
454 b8af7c06 2022-03-15 stsp print_colored = 1;
455 b8af7c06 2022-03-15 stsp print_found = 1;
456 b8af7c06 2022-03-15 stsp a->last_nfound = nfound;
457 b8af7c06 2022-03-15 stsp }
458 b8af7c06 2022-03-15 stsp
459 b8af7c06 2022-03-15 stsp if (a->last_ntrees != ntrees) {
460 b8af7c06 2022-03-15 stsp print_colored = 1;
461 b8af7c06 2022-03-15 stsp print_found = 1;
462 b8af7c06 2022-03-15 stsp print_trees = 1;
463 b8af7c06 2022-03-15 stsp a->last_ntrees = ntrees;
464 b8af7c06 2022-03-15 stsp }
465 b8af7c06 2022-03-15 stsp
466 b8af7c06 2022-03-15 stsp if ((print_colored || print_found || print_trees) &&
467 b8af7c06 2022-03-15 stsp !a->loading_done) {
468 b8af7c06 2022-03-15 stsp printf("\r");
469 b8af7c06 2022-03-15 stsp print_load_info(print_colored, print_found, print_trees,
470 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees);
471 b8af7c06 2022-03-15 stsp a->printed_something = 1;
472 b8af7c06 2022-03-15 stsp fflush(stdout);
473 05118f5a 2021-06-22 stsp return NULL;
474 b8af7c06 2022-03-15 stsp } else if (!a->loading_done) {
475 b8af7c06 2022-03-15 stsp printf("\r");
476 b8af7c06 2022-03-15 stsp print_load_info(1, 1, 1, ncolored, nfound, ntrees);
477 b8af7c06 2022-03-15 stsp printf("\n");
478 b8af7c06 2022-03-15 stsp a->loading_done = 1;
479 b8af7c06 2022-03-15 stsp }
480 05118f5a 2021-06-22 stsp
481 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
482 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
483 05118f5a 2021-06-22 stsp
484 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
485 05118f5a 2021-06-22 stsp print_searching = 1;
486 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
487 05118f5a 2021-06-22 stsp }
488 05118f5a 2021-06-22 stsp
489 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
490 05118f5a 2021-06-22 stsp print_searching = 1;
491 05118f5a 2021-06-22 stsp print_total = 1;
492 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
493 05118f5a 2021-06-22 stsp }
494 05118f5a 2021-06-22 stsp
495 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
496 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
497 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
498 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
499 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
500 05118f5a 2021-06-22 stsp }
501 05118f5a 2021-06-22 stsp
502 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
503 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
504 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
505 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
506 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
507 05118f5a 2021-06-22 stsp print_searching = 1;
508 05118f5a 2021-06-22 stsp print_total = 1;
509 05118f5a 2021-06-22 stsp print_deltify = 1;
510 05118f5a 2021-06-22 stsp }
511 05118f5a 2021-06-22 stsp }
512 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
513 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
514 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
515 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
516 05118f5a 2021-06-22 stsp print_searching = 1;
517 05118f5a 2021-06-22 stsp print_total = 1;
518 05118f5a 2021-06-22 stsp print_deltify = 1;
519 05118f5a 2021-06-22 stsp print_written = 1;
520 05118f5a 2021-06-22 stsp }
521 05118f5a 2021-06-22 stsp }
522 05118f5a 2021-06-22 stsp }
523 05118f5a 2021-06-22 stsp
524 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
525 05118f5a 2021-06-22 stsp printf("\r");
526 05118f5a 2021-06-22 stsp if (print_searching)
527 05118f5a 2021-06-22 stsp printf("packing %d reference%s", ncommits,
528 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
529 05118f5a 2021-06-22 stsp if (print_total)
530 05118f5a 2021-06-22 stsp printf("; %d object%s", nobj_total,
531 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
532 05118f5a 2021-06-22 stsp if (print_deltify)
533 05118f5a 2021-06-22 stsp printf("; deltify: %d%%", p_deltify);
534 05118f5a 2021-06-22 stsp if (print_written)
535 b5934965 2022-02-12 naddy printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
536 05118f5a 2021-06-22 stsp scaled_size, p_written);
537 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
538 05118f5a 2021-06-22 stsp print_written) {
539 05118f5a 2021-06-22 stsp a->printed_something = 1;
540 05118f5a 2021-06-22 stsp fflush(stdout);
541 05118f5a 2021-06-22 stsp }
542 05118f5a 2021-06-22 stsp return NULL;
543 05118f5a 2021-06-22 stsp }
544 05118f5a 2021-06-22 stsp
545 05118f5a 2021-06-22 stsp static const struct got_error *
546 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
547 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
548 05118f5a 2021-06-22 stsp {
549 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
550 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
551 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
552 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
553 05118f5a 2021-06-22 stsp
554 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
555 05118f5a 2021-06-22 stsp return NULL;
556 05118f5a 2021-06-22 stsp
557 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
558 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
559 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
560 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
561 05118f5a 2021-06-22 stsp print_size = 1;
562 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
563 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
564 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
565 05118f5a 2021-06-22 stsp }
566 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
567 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
568 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
569 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
570 05118f5a 2021-06-22 stsp print_indexed = 1;
571 05118f5a 2021-06-22 stsp print_size = 1;
572 05118f5a 2021-06-22 stsp }
573 05118f5a 2021-06-22 stsp }
574 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
575 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
576 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
577 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
578 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
579 05118f5a 2021-06-22 stsp print_resolved = 1;
580 05118f5a 2021-06-22 stsp print_indexed = 1;
581 05118f5a 2021-06-22 stsp print_size = 1;
582 05118f5a 2021-06-22 stsp }
583 05118f5a 2021-06-22 stsp }
584 05118f5a 2021-06-22 stsp
585 05118f5a 2021-06-22 stsp }
586 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
587 05118f5a 2021-06-22 stsp printf("\r");
588 05118f5a 2021-06-22 stsp if (print_size)
589 b5934965 2022-02-12 naddy printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
590 05118f5a 2021-06-22 stsp if (print_indexed)
591 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
592 05118f5a 2021-06-22 stsp if (print_resolved)
593 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
594 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
595 05118f5a 2021-06-22 stsp fflush(stdout);
596 05118f5a 2021-06-22 stsp
597 05118f5a 2021-06-22 stsp return NULL;
598 20662ea0 2021-04-10 stsp }
599 05118f5a 2021-06-22 stsp
600 05118f5a 2021-06-22 stsp static const struct got_error *
601 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
602 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
603 05118f5a 2021-06-22 stsp {
604 05118f5a 2021-06-22 stsp const struct got_error *err;
605 05118f5a 2021-06-22 stsp struct got_reference *ref;
606 05118f5a 2021-06-22 stsp
607 05118f5a 2021-06-22 stsp *new = NULL;
608 05118f5a 2021-06-22 stsp
609 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
610 05118f5a 2021-06-22 stsp if (err) {
611 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
612 05118f5a 2021-06-22 stsp return err;
613 05118f5a 2021-06-22 stsp
614 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
615 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
616 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
617 05118f5a 2021-06-22 stsp } else {
618 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
619 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
620 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
621 05118f5a 2021-06-22 stsp got_ref_close(ref);
622 05118f5a 2021-06-22 stsp }
623 05118f5a 2021-06-22 stsp
624 05118f5a 2021-06-22 stsp return err;
625 05118f5a 2021-06-22 stsp }
626 05118f5a 2021-06-22 stsp
627 05118f5a 2021-06-22 stsp static const struct got_error *
628 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
629 05118f5a 2021-06-22 stsp {
630 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
631 7d69d862 2021-11-15 stsp char *repo_path = NULL;
632 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
633 20e420c8 2022-04-11 stsp int ch, i, loose_obj_only = 1, verbosity = 0;
634 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
635 05118f5a 2021-06-22 stsp char *id_str = NULL;
636 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
637 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
638 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
639 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
640 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
641 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
642 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
643 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
644 05118f5a 2021-06-22 stsp
645 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
646 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
647 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
648 05118f5a 2021-06-22 stsp
649 20e420c8 2022-04-11 stsp while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
650 05118f5a 2021-06-22 stsp switch (ch) {
651 05118f5a 2021-06-22 stsp case 'a':
652 05118f5a 2021-06-22 stsp loose_obj_only = 0;
653 05118f5a 2021-06-22 stsp break;
654 05118f5a 2021-06-22 stsp case 'r':
655 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
656 05118f5a 2021-06-22 stsp if (repo_path == NULL)
657 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
658 05118f5a 2021-06-22 stsp optarg);
659 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
660 05118f5a 2021-06-22 stsp break;
661 05118f5a 2021-06-22 stsp case 'x':
662 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
663 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
664 05118f5a 2021-06-22 stsp optarg, NULL);
665 05118f5a 2021-06-22 stsp if (error)
666 05118f5a 2021-06-22 stsp return error;
667 20e420c8 2022-04-11 stsp break;
668 20e420c8 2022-04-11 stsp case 'q':
669 20e420c8 2022-04-11 stsp verbosity = -1;
670 05118f5a 2021-06-22 stsp break;
671 05118f5a 2021-06-22 stsp default:
672 05118f5a 2021-06-22 stsp usage_pack();
673 05118f5a 2021-06-22 stsp /* NOTREACHED */
674 05118f5a 2021-06-22 stsp }
675 05118f5a 2021-06-22 stsp }
676 05118f5a 2021-06-22 stsp
677 05118f5a 2021-06-22 stsp argc -= optind;
678 05118f5a 2021-06-22 stsp argv += optind;
679 05118f5a 2021-06-22 stsp
680 05118f5a 2021-06-22 stsp #ifndef PROFILE
681 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
682 05118f5a 2021-06-22 stsp NULL) == -1)
683 05118f5a 2021-06-22 stsp err(1, "pledge");
684 05118f5a 2021-06-22 stsp #endif
685 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
686 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
687 7d69d862 2021-11-15 stsp if (error)
688 7d69d862 2021-11-15 stsp goto done;
689 05118f5a 2021-06-22 stsp }
690 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
691 0ae84acc 2022-06-15 tracey if (error != NULL)
692 0ae84acc 2022-06-15 tracey goto done;
693 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
694 05118f5a 2021-06-22 stsp if (error)
695 05118f5a 2021-06-22 stsp goto done;
696 05118f5a 2021-06-22 stsp
697 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
698 05118f5a 2021-06-22 stsp if (error)
699 05118f5a 2021-06-22 stsp goto done;
700 05118f5a 2021-06-22 stsp
701 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
702 05118f5a 2021-06-22 stsp const char *refname = pe->path;
703 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
704 05118f5a 2021-06-22 stsp if (error)
705 05118f5a 2021-06-22 stsp goto done;
706 05118f5a 2021-06-22 stsp
707 05118f5a 2021-06-22 stsp }
708 05118f5a 2021-06-22 stsp
709 05118f5a 2021-06-22 stsp if (argc == 0) {
710 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
711 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
712 05118f5a 2021-06-22 stsp if (error)
713 05118f5a 2021-06-22 stsp goto done;
714 05118f5a 2021-06-22 stsp } else {
715 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
716 05118f5a 2021-06-22 stsp const char *refname;
717 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
718 05118f5a 2021-06-22 stsp refname = argv[i];
719 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
720 05118f5a 2021-06-22 stsp if (error)
721 05118f5a 2021-06-22 stsp goto done;
722 05118f5a 2021-06-22 stsp }
723 05118f5a 2021-06-22 stsp }
724 05118f5a 2021-06-22 stsp
725 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
726 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
727 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
728 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
729 05118f5a 2021-06-22 stsp continue;
730 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
731 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
732 05118f5a 2021-06-22 stsp free(re);
733 05118f5a 2021-06-22 stsp }
734 05118f5a 2021-06-22 stsp
735 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
736 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
737 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
738 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
739 20e420c8 2022-04-11 stsp ppa.verbosity = verbosity;
740 05118f5a 2021-06-22 stsp
741 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
742 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
743 05118f5a 2021-06-22 stsp pack_progress, &ppa, check_cancelled, NULL);
744 05118f5a 2021-06-22 stsp if (error) {
745 05118f5a 2021-06-22 stsp if (ppa.printed_something)
746 05118f5a 2021-06-22 stsp printf("\n");
747 05118f5a 2021-06-22 stsp goto done;
748 05118f5a 2021-06-22 stsp }
749 05118f5a 2021-06-22 stsp
750 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
751 05118f5a 2021-06-22 stsp if (error)
752 05118f5a 2021-06-22 stsp goto done;
753 20e420c8 2022-04-11 stsp if (verbosity >= 0)
754 20e420c8 2022-04-11 stsp printf("\nWrote %s.pack\n", id_str);
755 05118f5a 2021-06-22 stsp
756 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
757 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
758 05118f5a 2021-06-22 stsp if (error)
759 05118f5a 2021-06-22 stsp goto done;
760 20e420c8 2022-04-11 stsp if (verbosity >= 0)
761 20e420c8 2022-04-11 stsp printf("\nIndexed %s.pack\n", id_str);
762 05118f5a 2021-06-22 stsp done:
763 f8eebdd4 2021-10-15 stsp if (repo)
764 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
765 0ae84acc 2022-06-15 tracey if (pack_fds) {
766 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
767 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
768 0ae84acc 2022-06-15 tracey if (error == NULL)
769 0ae84acc 2022-06-15 tracey error = pack_err;
770 0ae84acc 2022-06-15 tracey }
771 05118f5a 2021-06-22 stsp got_pathlist_free(&exclude_args);
772 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
773 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
774 05118f5a 2021-06-22 stsp free(id_str);
775 05118f5a 2021-06-22 stsp free(pack_hash);
776 7d69d862 2021-11-15 stsp free(repo_path);
777 05118f5a 2021-06-22 stsp return error;
778 05118f5a 2021-06-22 stsp }
779 05118f5a 2021-06-22 stsp
780 05118f5a 2021-06-22 stsp __dead static void
781 05118f5a 2021-06-22 stsp usage_indexpack(void)
782 05118f5a 2021-06-22 stsp {
783 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
784 05118f5a 2021-06-22 stsp getprogname());
785 05118f5a 2021-06-22 stsp exit(1);
786 05118f5a 2021-06-22 stsp }
787 05118f5a 2021-06-22 stsp
788 05118f5a 2021-06-22 stsp static const struct got_error *
789 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
790 05118f5a 2021-06-22 stsp {
791 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
792 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
793 05118f5a 2021-06-22 stsp int ch;
794 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
795 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
796 05118f5a 2021-06-22 stsp char *id_str = NULL;
797 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
798 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
799 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
800 05118f5a 2021-06-22 stsp
801 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
802 05118f5a 2021-06-22 stsp switch (ch) {
803 05118f5a 2021-06-22 stsp default:
804 05118f5a 2021-06-22 stsp usage_indexpack();
805 05118f5a 2021-06-22 stsp /* NOTREACHED */
806 05118f5a 2021-06-22 stsp }
807 05118f5a 2021-06-22 stsp }
808 05118f5a 2021-06-22 stsp
809 05118f5a 2021-06-22 stsp argc -= optind;
810 05118f5a 2021-06-22 stsp argv += optind;
811 05118f5a 2021-06-22 stsp
812 05118f5a 2021-06-22 stsp if (argc != 1)
813 05118f5a 2021-06-22 stsp usage_indexpack();
814 05118f5a 2021-06-22 stsp
815 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
816 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
817 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
818 05118f5a 2021-06-22 stsp
819 05118f5a 2021-06-22 stsp #ifndef PROFILE
820 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
821 05118f5a 2021-06-22 stsp NULL) == -1)
822 05118f5a 2021-06-22 stsp err(1, "pledge");
823 05118f5a 2021-06-22 stsp #endif
824 05118f5a 2021-06-22 stsp
825 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
826 0ae84acc 2022-06-15 tracey if (error != NULL)
827 0ae84acc 2022-06-15 tracey goto done;
828 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
829 05118f5a 2021-06-22 stsp if (error)
830 05118f5a 2021-06-22 stsp goto done;
831 05118f5a 2021-06-22 stsp
832 802c0f04 2021-10-15 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
833 05118f5a 2021-06-22 stsp if (error)
834 05118f5a 2021-06-22 stsp goto done;
835 05118f5a 2021-06-22 stsp
836 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
837 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
838 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
839 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
840 05118f5a 2021-06-22 stsp
841 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
842 05118f5a 2021-06-22 stsp packfile_path);
843 05118f5a 2021-06-22 stsp if (error)
844 05118f5a 2021-06-22 stsp goto done;
845 05118f5a 2021-06-22 stsp
846 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
847 05118f5a 2021-06-22 stsp if (error)
848 05118f5a 2021-06-22 stsp goto done;
849 05118f5a 2021-06-22 stsp
850 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
851 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
852 05118f5a 2021-06-22 stsp if (error)
853 05118f5a 2021-06-22 stsp goto done;
854 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
855 05118f5a 2021-06-22 stsp done:
856 f8eebdd4 2021-10-15 stsp if (repo)
857 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
858 0ae84acc 2022-06-15 tracey if (pack_fds) {
859 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
860 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
861 0ae84acc 2022-06-15 tracey if (error == NULL)
862 0ae84acc 2022-06-15 tracey error = pack_err;
863 0ae84acc 2022-06-15 tracey }
864 05118f5a 2021-06-22 stsp free(id_str);
865 05118f5a 2021-06-22 stsp free(pack_hash);
866 05118f5a 2021-06-22 stsp return error;
867 05118f5a 2021-06-22 stsp }
868 05118f5a 2021-06-22 stsp
869 05118f5a 2021-06-22 stsp __dead static void
870 05118f5a 2021-06-22 stsp usage_listpack(void)
871 05118f5a 2021-06-22 stsp {
872 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
873 05118f5a 2021-06-22 stsp getprogname());
874 05118f5a 2021-06-22 stsp exit(1);
875 05118f5a 2021-06-22 stsp }
876 05118f5a 2021-06-22 stsp
877 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
878 05118f5a 2021-06-22 stsp int nblobs;
879 05118f5a 2021-06-22 stsp int ntrees;
880 05118f5a 2021-06-22 stsp int ncommits;
881 05118f5a 2021-06-22 stsp int ntags;
882 05118f5a 2021-06-22 stsp int noffdeltas;
883 05118f5a 2021-06-22 stsp int nrefdeltas;
884 05118f5a 2021-06-22 stsp int human_readable;
885 05118f5a 2021-06-22 stsp };
886 05118f5a 2021-06-22 stsp
887 05118f5a 2021-06-22 stsp static const struct got_error *
888 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
889 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
890 05118f5a 2021-06-22 stsp {
891 05118f5a 2021-06-22 stsp const struct got_error *err;
892 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
893 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
894 05118f5a 2021-06-22 stsp const char *type_str;
895 05118f5a 2021-06-22 stsp
896 0ae84acc 2022-06-15 tracey err = got_object_id_str(&id_str, id);
897 05118f5a 2021-06-22 stsp if (err)
898 05118f5a 2021-06-22 stsp return err;
899 05118f5a 2021-06-22 stsp
900 05118f5a 2021-06-22 stsp switch (type) {
901 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
902 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
903 05118f5a 2021-06-22 stsp a->nblobs++;
904 05118f5a 2021-06-22 stsp break;
905 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
906 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
907 05118f5a 2021-06-22 stsp a->ntrees++;
908 05118f5a 2021-06-22 stsp break;
909 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
910 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
911 05118f5a 2021-06-22 stsp a->ncommits++;
912 05118f5a 2021-06-22 stsp break;
913 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
914 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
915 05118f5a 2021-06-22 stsp a->ntags++;
916 05118f5a 2021-06-22 stsp break;
917 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
918 05118f5a 2021-06-22 stsp type_str = "offset-delta";
919 963ac08a 2021-09-25 naddy if (asprintf(&delta_str, " base-offset %lld",
920 963ac08a 2021-09-25 naddy (long long)base_offset) == -1) {
921 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
922 05118f5a 2021-06-22 stsp goto done;
923 05118f5a 2021-06-22 stsp }
924 05118f5a 2021-06-22 stsp a->noffdeltas++;
925 05118f5a 2021-06-22 stsp break;
926 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
927 05118f5a 2021-06-22 stsp type_str = "ref-delta";
928 05118f5a 2021-06-22 stsp err = got_object_id_str(&base_id_str, base_id);
929 05118f5a 2021-06-22 stsp if (err)
930 05118f5a 2021-06-22 stsp goto done;
931 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
932 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
933 05118f5a 2021-06-22 stsp goto done;
934 05118f5a 2021-06-22 stsp }
935 05118f5a 2021-06-22 stsp a->nrefdeltas++;
936 05118f5a 2021-06-22 stsp break;
937 05118f5a 2021-06-22 stsp default:
938 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
939 05118f5a 2021-06-22 stsp goto done;
940 05118f5a 2021-06-22 stsp }
941 05118f5a 2021-06-22 stsp if (a->human_readable) {
942 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
943 05118f5a 2021-06-22 stsp char *s;;
944 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
945 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
946 05118f5a 2021-06-22 stsp goto done;
947 05118f5a 2021-06-22 stsp }
948 05118f5a 2021-06-22 stsp s = scaled;
949 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
950 05118f5a 2021-06-22 stsp s++;
951 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %s%s\n", id_str, type_str,
952 963ac08a 2021-09-25 naddy (long long)offset, s, delta_str ? delta_str : "");
953 05118f5a 2021-06-22 stsp } else {
954 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %lld%s\n", id_str, type_str,
955 963ac08a 2021-09-25 naddy (long long)offset, (long long)size,
956 963ac08a 2021-09-25 naddy delta_str ? delta_str : "");
957 05118f5a 2021-06-22 stsp }
958 05118f5a 2021-06-22 stsp done:
959 05118f5a 2021-06-22 stsp free(id_str);
960 05118f5a 2021-06-22 stsp free(base_id_str);
961 05118f5a 2021-06-22 stsp free(delta_str);
962 05118f5a 2021-06-22 stsp return err;
963 05118f5a 2021-06-22 stsp }
964 05118f5a 2021-06-22 stsp
965 05118f5a 2021-06-22 stsp static const struct got_error *
966 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
967 05118f5a 2021-06-22 stsp {
968 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
969 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
970 05118f5a 2021-06-22 stsp int ch;
971 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
972 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
973 05118f5a 2021-06-22 stsp char *id_str = NULL;
974 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
975 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
976 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
977 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
978 05118f5a 2021-06-22 stsp
979 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
980 05118f5a 2021-06-22 stsp switch (ch) {
981 05118f5a 2021-06-22 stsp case 'h':
982 05118f5a 2021-06-22 stsp human_readable = 1;
983 05118f5a 2021-06-22 stsp break;
984 05118f5a 2021-06-22 stsp case 's':
985 05118f5a 2021-06-22 stsp show_stats = 1;
986 05118f5a 2021-06-22 stsp break;
987 05118f5a 2021-06-22 stsp default:
988 05118f5a 2021-06-22 stsp usage_listpack();
989 05118f5a 2021-06-22 stsp /* NOTREACHED */
990 05118f5a 2021-06-22 stsp }
991 05118f5a 2021-06-22 stsp }
992 05118f5a 2021-06-22 stsp
993 05118f5a 2021-06-22 stsp argc -= optind;
994 05118f5a 2021-06-22 stsp argv += optind;
995 05118f5a 2021-06-22 stsp
996 05118f5a 2021-06-22 stsp if (argc != 1)
997 05118f5a 2021-06-22 stsp usage_listpack();
998 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
999 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
1000 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
1001 05118f5a 2021-06-22 stsp
1002 05118f5a 2021-06-22 stsp #ifndef PROFILE
1003 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1004 05118f5a 2021-06-22 stsp NULL) == -1)
1005 05118f5a 2021-06-22 stsp err(1, "pledge");
1006 05118f5a 2021-06-22 stsp #endif
1007 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1008 0ae84acc 2022-06-15 tracey if (error != NULL)
1009 0ae84acc 2022-06-15 tracey goto done;
1010 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1011 05118f5a 2021-06-22 stsp if (error)
1012 05118f5a 2021-06-22 stsp goto done;
1013 57160834 2022-05-31 stsp #ifndef PROFILE
1014 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
1015 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1016 57160834 2022-05-31 stsp NULL) == -1)
1017 57160834 2022-05-31 stsp err(1, "pledge");
1018 57160834 2022-05-31 stsp #endif
1019 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1020 05118f5a 2021-06-22 stsp if (error)
1021 05118f5a 2021-06-22 stsp goto done;
1022 05118f5a 2021-06-22 stsp
1023 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
1024 05118f5a 2021-06-22 stsp packfile_path);
1025 05118f5a 2021-06-22 stsp if (error)
1026 05118f5a 2021-06-22 stsp goto done;
1027 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
1028 05118f5a 2021-06-22 stsp if (error)
1029 05118f5a 2021-06-22 stsp goto done;
1030 05118f5a 2021-06-22 stsp
1031 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
1032 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
1033 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
1034 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
1035 05118f5a 2021-06-22 stsp if (error)
1036 05118f5a 2021-06-22 stsp goto done;
1037 05118f5a 2021-06-22 stsp if (show_stats) {
1038 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1039 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1040 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1041 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
1042 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1043 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
1044 05118f5a 2021-06-22 stsp }
1045 05118f5a 2021-06-22 stsp done:
1046 f8eebdd4 2021-10-15 stsp if (repo)
1047 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
1048 0ae84acc 2022-06-15 tracey if (pack_fds) {
1049 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1050 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1051 0ae84acc 2022-06-15 tracey if (error == NULL)
1052 0ae84acc 2022-06-15 tracey error = pack_err;
1053 0ae84acc 2022-06-15 tracey }
1054 05118f5a 2021-06-22 stsp free(id_str);
1055 05118f5a 2021-06-22 stsp free(pack_hash);
1056 05118f5a 2021-06-22 stsp free(packfile_path);
1057 05118f5a 2021-06-22 stsp return error;
1058 b3d68e7f 2021-07-03 stsp }
1059 b3d68e7f 2021-07-03 stsp
1060 b3d68e7f 2021-07-03 stsp __dead static void
1061 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1062 b3d68e7f 2021-07-03 stsp {
1063 ef8ec606 2021-07-27 stsp fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1064 1124fe40 2021-07-07 stsp "[-q]\n", getprogname());
1065 b3d68e7f 2021-07-03 stsp exit(1);
1066 b3d68e7f 2021-07-03 stsp }
1067 b3d68e7f 2021-07-03 stsp
1068 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1069 b3d68e7f 2021-07-03 stsp int last_nloose;
1070 b3d68e7f 2021-07-03 stsp int last_ncommits;
1071 b3d68e7f 2021-07-03 stsp int last_npurged;
1072 b3d68e7f 2021-07-03 stsp int verbosity;
1073 b3d68e7f 2021-07-03 stsp int printed_something;
1074 b3d68e7f 2021-07-03 stsp int dry_run;
1075 b3d68e7f 2021-07-03 stsp };
1076 b3d68e7f 2021-07-03 stsp
1077 b3d68e7f 2021-07-03 stsp static const struct got_error *
1078 b3d68e7f 2021-07-03 stsp cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1079 b3d68e7f 2021-07-03 stsp {
1080 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1081 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1082 b3d68e7f 2021-07-03 stsp
1083 b3d68e7f 2021-07-03 stsp if (a->last_nloose != nloose) {
1084 b3d68e7f 2021-07-03 stsp print_loose = 1;
1085 b3d68e7f 2021-07-03 stsp a->last_nloose = nloose;
1086 b3d68e7f 2021-07-03 stsp }
1087 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1088 b3d68e7f 2021-07-03 stsp print_loose = 1;
1089 b3d68e7f 2021-07-03 stsp print_commits = 1;
1090 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1091 b3d68e7f 2021-07-03 stsp }
1092 b3d68e7f 2021-07-03 stsp if (a->last_npurged != npurged) {
1093 b3d68e7f 2021-07-03 stsp print_loose = 1;
1094 b3d68e7f 2021-07-03 stsp print_commits = 1;
1095 b3d68e7f 2021-07-03 stsp print_purged = 1;
1096 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1097 b3d68e7f 2021-07-03 stsp }
1098 b3d68e7f 2021-07-03 stsp
1099 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1100 b3d68e7f 2021-07-03 stsp return NULL;
1101 b3d68e7f 2021-07-03 stsp
1102 b3d68e7f 2021-07-03 stsp if (print_loose || print_commits || print_purged)
1103 b3d68e7f 2021-07-03 stsp printf("\r");
1104 b3d68e7f 2021-07-03 stsp if (print_loose)
1105 b3d68e7f 2021-07-03 stsp printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1106 b3d68e7f 2021-07-03 stsp if (print_commits)
1107 b3d68e7f 2021-07-03 stsp printf("; %d commit%s scanned", ncommits,
1108 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1109 b3d68e7f 2021-07-03 stsp if (print_purged) {
1110 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1111 b3d68e7f 2021-07-03 stsp printf("; %d object%s could be purged", npurged,
1112 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1113 b3d68e7f 2021-07-03 stsp } else {
1114 b3d68e7f 2021-07-03 stsp printf("; %d object%s purged", npurged,
1115 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1116 b3d68e7f 2021-07-03 stsp }
1117 b3d68e7f 2021-07-03 stsp }
1118 b3d68e7f 2021-07-03 stsp if (print_loose || print_commits || print_purged) {
1119 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1120 b3d68e7f 2021-07-03 stsp fflush(stdout);
1121 b3d68e7f 2021-07-03 stsp }
1122 b3d68e7f 2021-07-03 stsp return NULL;
1123 05118f5a 2021-06-22 stsp }
1124 b3d68e7f 2021-07-03 stsp
1125 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1126 1124fe40 2021-07-07 stsp int verbosity;
1127 1124fe40 2021-07-07 stsp int printed_something;
1128 1124fe40 2021-07-07 stsp int dry_run;
1129 1124fe40 2021-07-07 stsp };
1130 1124fe40 2021-07-07 stsp
1131 b3d68e7f 2021-07-03 stsp static const struct got_error *
1132 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1133 1124fe40 2021-07-07 stsp {
1134 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1135 1124fe40 2021-07-07 stsp
1136 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1137 1124fe40 2021-07-07 stsp return NULL;
1138 1124fe40 2021-07-07 stsp
1139 1124fe40 2021-07-07 stsp if (a->dry_run)
1140 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1141 1124fe40 2021-07-07 stsp else
1142 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1143 1124fe40 2021-07-07 stsp
1144 1124fe40 2021-07-07 stsp a->printed_something = 1;
1145 1124fe40 2021-07-07 stsp return NULL;
1146 1124fe40 2021-07-07 stsp }
1147 1124fe40 2021-07-07 stsp
1148 1124fe40 2021-07-07 stsp static const struct got_error *
1149 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1150 b3d68e7f 2021-07-03 stsp {
1151 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1152 7d69d862 2021-11-15 stsp char *repo_path = NULL;
1153 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1154 b3d68e7f 2021-07-03 stsp int ch, dry_run = 0, npacked = 0, verbosity = 0;
1155 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1156 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1157 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1158 b3d68e7f 2021-07-03 stsp off_t size_before, size_after;
1159 b3d68e7f 2021-07-03 stsp char scaled_before[FMT_SCALED_STRSIZE];
1160 b3d68e7f 2021-07-03 stsp char scaled_after[FMT_SCALED_STRSIZE];
1161 b3d68e7f 2021-07-03 stsp char scaled_diff[FMT_SCALED_STRSIZE];
1162 9188bd78 2021-07-03 stsp char **extensions;
1163 9188bd78 2021-07-03 stsp int nextensions, i;
1164 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
1165 b3d68e7f 2021-07-03 stsp
1166 ef8ec606 2021-07-27 stsp while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1167 b3d68e7f 2021-07-03 stsp switch (ch) {
1168 ef8ec606 2021-07-27 stsp case 'a':
1169 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1170 ef8ec606 2021-07-27 stsp break;
1171 1124fe40 2021-07-07 stsp case 'p':
1172 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1173 1124fe40 2021-07-07 stsp break;
1174 b3d68e7f 2021-07-03 stsp case 'r':
1175 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1176 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1177 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1178 b3d68e7f 2021-07-03 stsp optarg);
1179 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1180 b3d68e7f 2021-07-03 stsp break;
1181 b3d68e7f 2021-07-03 stsp case 'n':
1182 b3d68e7f 2021-07-03 stsp dry_run = 1;
1183 b3d68e7f 2021-07-03 stsp break;
1184 b3d68e7f 2021-07-03 stsp case 'q':
1185 b3d68e7f 2021-07-03 stsp verbosity = -1;
1186 b3d68e7f 2021-07-03 stsp break;
1187 b3d68e7f 2021-07-03 stsp default:
1188 b3d68e7f 2021-07-03 stsp usage_cleanup();
1189 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1190 b3d68e7f 2021-07-03 stsp }
1191 b3d68e7f 2021-07-03 stsp }
1192 b3d68e7f 2021-07-03 stsp
1193 b3d68e7f 2021-07-03 stsp argc -= optind;
1194 b3d68e7f 2021-07-03 stsp argv += optind;
1195 b3d68e7f 2021-07-03 stsp
1196 b3d68e7f 2021-07-03 stsp #ifndef PROFILE
1197 b3d68e7f 2021-07-03 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1198 b3d68e7f 2021-07-03 stsp NULL) == -1)
1199 b3d68e7f 2021-07-03 stsp err(1, "pledge");
1200 b3d68e7f 2021-07-03 stsp #endif
1201 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
1202 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
1203 7d69d862 2021-11-15 stsp if (error)
1204 7d69d862 2021-11-15 stsp goto done;
1205 b3d68e7f 2021-07-03 stsp }
1206 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1207 0ae84acc 2022-06-15 tracey if (error != NULL)
1208 0ae84acc 2022-06-15 tracey goto done;
1209 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1210 b3d68e7f 2021-07-03 stsp if (error)
1211 b3d68e7f 2021-07-03 stsp goto done;
1212 b3d68e7f 2021-07-03 stsp
1213 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1214 b3d68e7f 2021-07-03 stsp if (error)
1215 b3d68e7f 2021-07-03 stsp goto done;
1216 b3d68e7f 2021-07-03 stsp
1217 9188bd78 2021-07-03 stsp got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1218 9188bd78 2021-07-03 stsp repo);
1219 9188bd78 2021-07-03 stsp for (i = 0; i < nextensions; i++) {
1220 9188bd78 2021-07-03 stsp if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1221 9188bd78 2021-07-03 stsp error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1222 9188bd78 2021-07-03 stsp "the preciousObjects Git extension is enabled; "
1223 9188bd78 2021-07-03 stsp "this implies that objects must not be deleted");
1224 9188bd78 2021-07-03 stsp goto done;
1225 9188bd78 2021-07-03 stsp }
1226 9188bd78 2021-07-03 stsp }
1227 9188bd78 2021-07-03 stsp
1228 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1229 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1230 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1231 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1232 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1233 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1234 1124fe40 2021-07-07 stsp goto done;
1235 1124fe40 2021-07-07 stsp }
1236 1124fe40 2021-07-07 stsp
1237 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1238 b3d68e7f 2021-07-03 stsp cpa.last_ncommits = -1;
1239 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1240 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1241 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1242 b3d68e7f 2021-07-03 stsp error = got_repo_purge_unreferenced_loose_objects(repo,
1243 abc59930 2021-09-05 naddy &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1244 abc59930 2021-09-05 naddy cleanup_progress, &cpa, check_cancelled, NULL);
1245 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1246 b3d68e7f 2021-07-03 stsp printf("\n");
1247 b3d68e7f 2021-07-03 stsp if (error)
1248 b3d68e7f 2021-07-03 stsp goto done;
1249 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1250 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_before, scaled_before) == -1) {
1251 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1252 b3d68e7f 2021-07-03 stsp goto done;
1253 b3d68e7f 2021-07-03 stsp }
1254 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_after, scaled_after) == -1) {
1255 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1256 b3d68e7f 2021-07-03 stsp goto done;
1257 b3d68e7f 2021-07-03 stsp }
1258 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1259 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1260 b3d68e7f 2021-07-03 stsp goto done;
1261 b3d68e7f 2021-07-03 stsp }
1262 b3d68e7f 2021-07-03 stsp printf("loose total size before: %s\n", scaled_before);
1263 b3d68e7f 2021-07-03 stsp printf("loose total size after: %s\n", scaled_after);
1264 b3d68e7f 2021-07-03 stsp if (dry_run) {
1265 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1266 b3d68e7f 2021-07-03 stsp scaled_diff);
1267 b3d68e7f 2021-07-03 stsp } else
1268 b3d68e7f 2021-07-03 stsp printf("disk space freed: %s\n", scaled_diff);
1269 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1270 b3d68e7f 2021-07-03 stsp }
1271 b3d68e7f 2021-07-03 stsp done:
1272 b3d68e7f 2021-07-03 stsp if (repo)
1273 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1274 0ae84acc 2022-06-15 tracey if (pack_fds) {
1275 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1276 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1277 0ae84acc 2022-06-15 tracey if (error == NULL)
1278 0ae84acc 2022-06-15 tracey error = pack_err;
1279 0ae84acc 2022-06-15 tracey }
1280 7d69d862 2021-11-15 stsp free(repo_path);
1281 b3d68e7f 2021-07-03 stsp return error;
1282 b3d68e7f 2021-07-03 stsp }