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