Blame


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