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