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 2df845d5 2023-07-07 op #include "got_repository_dump.h"
43 f7d653fc 2023-07-09 op #include "got_repository_load.h"
44 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
45 20662ea0 2021-04-10 stsp #include "got_path.h"
46 20662ea0 2021-04-10 stsp #include "got_privsep.h"
47 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
48 7d69d862 2021-11-15 stsp #include "got_worktree.h"
49 20662ea0 2021-04-10 stsp
50 20662ea0 2021-04-10 stsp #ifndef nitems
51 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 20662ea0 2021-04-10 stsp #endif
53 20662ea0 2021-04-10 stsp
54 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
55 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
56 20662ea0 2021-04-10 stsp
57 20662ea0 2021-04-10 stsp static void
58 20662ea0 2021-04-10 stsp catch_sigint(int signo)
59 20662ea0 2021-04-10 stsp {
60 20662ea0 2021-04-10 stsp sigint_received = 1;
61 20662ea0 2021-04-10 stsp }
62 20662ea0 2021-04-10 stsp
63 20662ea0 2021-04-10 stsp static void
64 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
65 20662ea0 2021-04-10 stsp {
66 20662ea0 2021-04-10 stsp sigpipe_received = 1;
67 20662ea0 2021-04-10 stsp }
68 20662ea0 2021-04-10 stsp
69 05118f5a 2021-06-22 stsp static const struct got_error *
70 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
71 05118f5a 2021-06-22 stsp {
72 05118f5a 2021-06-22 stsp if (sigint_received || sigpipe_received)
73 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_CANCELLED);
74 05118f5a 2021-06-22 stsp return NULL;
75 05118f5a 2021-06-22 stsp }
76 20662ea0 2021-04-10 stsp
77 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
78 20662ea0 2021-04-10 stsp const char *cmd_name;
79 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
81 20662ea0 2021-04-10 stsp const char *cmd_alias;
82 20662ea0 2021-04-10 stsp };
83 20662ea0 2021-04-10 stsp
84 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
85 02a5c5d0 2022-07-04 stsp __dead static void usage_init(void);
86 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
87 05118f5a 2021-06-22 stsp __dead static void usage_pack(void);
88 05118f5a 2021-06-22 stsp __dead static void usage_indexpack(void);
89 05118f5a 2021-06-22 stsp __dead static void usage_listpack(void);
90 b3d68e7f 2021-07-03 stsp __dead static void usage_cleanup(void);
91 2df845d5 2023-07-07 op __dead static void usage_dump(void);
92 f7d653fc 2023-07-09 op __dead static void usage_load(void);
93 20662ea0 2021-04-10 stsp
94 02a5c5d0 2022-07-04 stsp static const struct got_error* cmd_init(int, char *[]);
95 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
96 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
97 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
98 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
99 b3d68e7f 2021-07-03 stsp static const struct got_error* cmd_cleanup(int, char *[]);
100 2df845d5 2023-07-07 op static const struct got_error* cmd_dump(int, char *[]);
101 f7d653fc 2023-07-09 op static const struct got_error* cmd_load(int, char *[]);
102 20662ea0 2021-04-10 stsp
103 3e166534 2022-02-16 naddy static const struct gotadmin_cmd gotadmin_commands[] = {
104 02a5c5d0 2022-07-04 stsp { "init", cmd_init, usage_init, "" },
105 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
106 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
107 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
108 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
109 b3d68e7f 2021-07-03 stsp { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
110 2df845d5 2023-07-07 op { "dump", cmd_dump, usage_dump, "" },
111 f7d653fc 2023-07-09 op { "load", cmd_load, usage_load, "" },
112 20662ea0 2021-04-10 stsp };
113 20662ea0 2021-04-10 stsp
114 20662ea0 2021-04-10 stsp static void
115 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
116 20662ea0 2021-04-10 stsp {
117 20662ea0 2021-04-10 stsp size_t i;
118 20662ea0 2021-04-10 stsp
119 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
120 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
121 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
122 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
123 20662ea0 2021-04-10 stsp }
124 20662ea0 2021-04-10 stsp fputc('\n', fp);
125 20662ea0 2021-04-10 stsp }
126 20662ea0 2021-04-10 stsp
127 20662ea0 2021-04-10 stsp int
128 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
129 20662ea0 2021-04-10 stsp {
130 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd;
131 20662ea0 2021-04-10 stsp size_t i;
132 20662ea0 2021-04-10 stsp int ch;
133 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
134 3e166534 2022-02-16 naddy static const struct option longopts[] = {
135 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
136 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
137 20662ea0 2021-04-10 stsp };
138 20662ea0 2021-04-10 stsp
139 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
140 20662ea0 2021-04-10 stsp
141 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
142 20662ea0 2021-04-10 stsp switch (ch) {
143 20662ea0 2021-04-10 stsp case 'h':
144 20662ea0 2021-04-10 stsp hflag = 1;
145 20662ea0 2021-04-10 stsp break;
146 20662ea0 2021-04-10 stsp case 'V':
147 20662ea0 2021-04-10 stsp Vflag = 1;
148 20662ea0 2021-04-10 stsp break;
149 20662ea0 2021-04-10 stsp default:
150 20662ea0 2021-04-10 stsp usage(hflag, 1);
151 20662ea0 2021-04-10 stsp /* NOTREACHED */
152 20662ea0 2021-04-10 stsp }
153 20662ea0 2021-04-10 stsp }
154 20662ea0 2021-04-10 stsp
155 20662ea0 2021-04-10 stsp argc -= optind;
156 20662ea0 2021-04-10 stsp argv += optind;
157 20662ea0 2021-04-10 stsp optind = 1;
158 20662ea0 2021-04-10 stsp optreset = 1;
159 20662ea0 2021-04-10 stsp
160 20662ea0 2021-04-10 stsp if (Vflag) {
161 20662ea0 2021-04-10 stsp got_version_print_str();
162 20662ea0 2021-04-10 stsp return 0;
163 20662ea0 2021-04-10 stsp }
164 20662ea0 2021-04-10 stsp
165 20662ea0 2021-04-10 stsp if (argc <= 0)
166 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
167 20662ea0 2021-04-10 stsp
168 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
169 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
170 20662ea0 2021-04-10 stsp
171 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
172 20662ea0 2021-04-10 stsp const struct got_error *error;
173 20662ea0 2021-04-10 stsp
174 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
175 20662ea0 2021-04-10 stsp
176 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
177 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
178 20662ea0 2021-04-10 stsp continue;
179 20662ea0 2021-04-10 stsp
180 20662ea0 2021-04-10 stsp if (hflag)
181 3e166534 2022-02-16 naddy cmd->cmd_usage();
182 20662ea0 2021-04-10 stsp
183 3e166534 2022-02-16 naddy error = cmd->cmd_main(argc, argv);
184 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
185 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
186 20662ea0 2021-04-10 stsp !(sigpipe_received &&
187 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
188 20662ea0 2021-04-10 stsp !(sigint_received &&
189 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
190 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
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 return 0;
195 20662ea0 2021-04-10 stsp }
196 20662ea0 2021-04-10 stsp
197 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
198 20662ea0 2021-04-10 stsp list_commands(stderr);
199 20662ea0 2021-04-10 stsp return 1;
200 20662ea0 2021-04-10 stsp }
201 20662ea0 2021-04-10 stsp
202 20662ea0 2021-04-10 stsp __dead static void
203 20662ea0 2021-04-10 stsp usage(int hflag, int status)
204 20662ea0 2021-04-10 stsp {
205 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
206 20662ea0 2021-04-10 stsp
207 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
208 20662ea0 2021-04-10 stsp getprogname());
209 20662ea0 2021-04-10 stsp if (hflag)
210 20662ea0 2021-04-10 stsp list_commands(fp);
211 20662ea0 2021-04-10 stsp exit(status);
212 20662ea0 2021-04-10 stsp }
213 20662ea0 2021-04-10 stsp
214 20662ea0 2021-04-10 stsp static const struct got_error *
215 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
216 20662ea0 2021-04-10 stsp {
217 20662ea0 2021-04-10 stsp const struct got_error *err;
218 20662ea0 2021-04-10 stsp
219 20662ea0 2021-04-10 stsp #ifdef PROFILE
220 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
221 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
222 20662ea0 2021-04-10 stsp #endif
223 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
224 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
225 20662ea0 2021-04-10 stsp
226 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
227 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
228 20662ea0 2021-04-10 stsp
229 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
230 20662ea0 2021-04-10 stsp if (err != NULL)
231 20662ea0 2021-04-10 stsp return err;
232 20662ea0 2021-04-10 stsp
233 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
234 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
235 20662ea0 2021-04-10 stsp
236 20662ea0 2021-04-10 stsp return NULL;
237 20662ea0 2021-04-10 stsp }
238 20662ea0 2021-04-10 stsp
239 20662ea0 2021-04-10 stsp __dead static void
240 20662ea0 2021-04-10 stsp usage_info(void)
241 20662ea0 2021-04-10 stsp {
242 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
243 20662ea0 2021-04-10 stsp getprogname());
244 20662ea0 2021-04-10 stsp exit(1);
245 20662ea0 2021-04-10 stsp }
246 20662ea0 2021-04-10 stsp
247 20662ea0 2021-04-10 stsp static const struct got_error *
248 7d69d862 2021-11-15 stsp get_repo_path(char **repo_path)
249 7d69d862 2021-11-15 stsp {
250 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
251 7d69d862 2021-11-15 stsp struct got_worktree *worktree = NULL;
252 7d69d862 2021-11-15 stsp char *cwd;
253 7d69d862 2021-11-15 stsp
254 7d69d862 2021-11-15 stsp *repo_path = NULL;
255 7d69d862 2021-11-15 stsp
256 7d69d862 2021-11-15 stsp cwd = getcwd(NULL, 0);
257 7d69d862 2021-11-15 stsp if (cwd == NULL)
258 7d69d862 2021-11-15 stsp return got_error_from_errno("getcwd");
259 7d69d862 2021-11-15 stsp
260 df6221c7 2023-07-19 stsp err = got_worktree_open(&worktree, cwd, NULL);
261 7d69d862 2021-11-15 stsp if (err) {
262 7d69d862 2021-11-15 stsp if (err->code != GOT_ERR_NOT_WORKTREE)
263 7d69d862 2021-11-15 stsp goto done;
264 7d69d862 2021-11-15 stsp err = NULL;
265 7d69d862 2021-11-15 stsp }
266 7d69d862 2021-11-15 stsp
267 7d69d862 2021-11-15 stsp if (worktree)
268 7d69d862 2021-11-15 stsp *repo_path = strdup(got_worktree_get_repo_path(worktree));
269 7d69d862 2021-11-15 stsp else
270 7d69d862 2021-11-15 stsp *repo_path = strdup(cwd);
271 7d69d862 2021-11-15 stsp if (*repo_path == NULL)
272 7d69d862 2021-11-15 stsp err = got_error_from_errno("strdup");
273 7d69d862 2021-11-15 stsp done:
274 7d69d862 2021-11-15 stsp if (worktree)
275 7d69d862 2021-11-15 stsp got_worktree_close(worktree);
276 7d69d862 2021-11-15 stsp free(cwd);
277 7d69d862 2021-11-15 stsp return err;
278 02a5c5d0 2022-07-04 stsp }
279 02a5c5d0 2022-07-04 stsp
280 02a5c5d0 2022-07-04 stsp __dead static void
281 02a5c5d0 2022-07-04 stsp usage_init(void)
282 02a5c5d0 2022-07-04 stsp {
283 6f04a73d 2022-09-20 mark fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
284 6f04a73d 2022-09-20 mark getprogname());
285 02a5c5d0 2022-07-04 stsp exit(1);
286 02a5c5d0 2022-07-04 stsp }
287 02a5c5d0 2022-07-04 stsp
288 02a5c5d0 2022-07-04 stsp static const struct got_error *
289 02a5c5d0 2022-07-04 stsp cmd_init(int argc, char *argv[])
290 02a5c5d0 2022-07-04 stsp {
291 02a5c5d0 2022-07-04 stsp const struct got_error *error = NULL;
292 6f04a73d 2022-09-20 mark const char *head_name = NULL;
293 02a5c5d0 2022-07-04 stsp char *repo_path = NULL;
294 02a5c5d0 2022-07-04 stsp int ch;
295 1fa0d17d 2023-02-13 op
296 1fa0d17d 2023-02-13 op #ifndef PROFILE
297 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
298 1fa0d17d 2023-02-13 op err(1, "pledge");
299 1fa0d17d 2023-02-13 op #endif
300 02a5c5d0 2022-07-04 stsp
301 6f04a73d 2022-09-20 mark while ((ch = getopt(argc, argv, "b:")) != -1) {
302 02a5c5d0 2022-07-04 stsp switch (ch) {
303 6f04a73d 2022-09-20 mark case 'b':
304 6f04a73d 2022-09-20 mark head_name = optarg;
305 6f04a73d 2022-09-20 mark break;
306 02a5c5d0 2022-07-04 stsp default:
307 02a5c5d0 2022-07-04 stsp usage_init();
308 02a5c5d0 2022-07-04 stsp /* NOTREACHED */
309 02a5c5d0 2022-07-04 stsp }
310 02a5c5d0 2022-07-04 stsp }
311 02a5c5d0 2022-07-04 stsp
312 02a5c5d0 2022-07-04 stsp argc -= optind;
313 02a5c5d0 2022-07-04 stsp argv += optind;
314 02a5c5d0 2022-07-04 stsp
315 02a5c5d0 2022-07-04 stsp if (argc != 1)
316 02a5c5d0 2022-07-04 stsp usage_init();
317 02a5c5d0 2022-07-04 stsp
318 02a5c5d0 2022-07-04 stsp repo_path = strdup(argv[0]);
319 02a5c5d0 2022-07-04 stsp if (repo_path == NULL)
320 02a5c5d0 2022-07-04 stsp return got_error_from_errno("strdup");
321 02a5c5d0 2022-07-04 stsp
322 02a5c5d0 2022-07-04 stsp got_path_strip_trailing_slashes(repo_path);
323 02a5c5d0 2022-07-04 stsp
324 02a5c5d0 2022-07-04 stsp error = got_path_mkdir(repo_path);
325 02a5c5d0 2022-07-04 stsp if (error &&
326 02a5c5d0 2022-07-04 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
327 02a5c5d0 2022-07-04 stsp goto done;
328 02a5c5d0 2022-07-04 stsp
329 02a5c5d0 2022-07-04 stsp error = apply_unveil(repo_path, 0);
330 02a5c5d0 2022-07-04 stsp if (error)
331 02a5c5d0 2022-07-04 stsp goto done;
332 02a5c5d0 2022-07-04 stsp
333 6f04a73d 2022-09-20 mark error = got_repo_init(repo_path, head_name);
334 02a5c5d0 2022-07-04 stsp done:
335 02a5c5d0 2022-07-04 stsp free(repo_path);
336 02a5c5d0 2022-07-04 stsp return error;
337 7d69d862 2021-11-15 stsp }
338 7d69d862 2021-11-15 stsp
339 7d69d862 2021-11-15 stsp static const struct got_error *
340 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
341 20662ea0 2021-04-10 stsp {
342 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
343 7d69d862 2021-11-15 stsp char *repo_path = NULL;
344 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
345 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
346 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
347 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
348 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
349 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
350 1fa0d17d 2023-02-13 op
351 1fa0d17d 2023-02-13 op #ifndef PROFILE
352 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
353 1fa0d17d 2023-02-13 op NULL) == -1)
354 1fa0d17d 2023-02-13 op err(1, "pledge");
355 1fa0d17d 2023-02-13 op #endif
356 20662ea0 2021-04-10 stsp
357 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
358 20662ea0 2021-04-10 stsp switch (ch) {
359 20662ea0 2021-04-10 stsp case 'r':
360 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
361 20662ea0 2021-04-10 stsp if (repo_path == NULL)
362 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
363 20662ea0 2021-04-10 stsp optarg);
364 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
365 20662ea0 2021-04-10 stsp break;
366 20662ea0 2021-04-10 stsp default:
367 20662ea0 2021-04-10 stsp usage_info();
368 20662ea0 2021-04-10 stsp /* NOTREACHED */
369 20662ea0 2021-04-10 stsp }
370 20662ea0 2021-04-10 stsp }
371 20662ea0 2021-04-10 stsp
372 20662ea0 2021-04-10 stsp argc -= optind;
373 20662ea0 2021-04-10 stsp argv += optind;
374 20662ea0 2021-04-10 stsp
375 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
376 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
377 7d69d862 2021-11-15 stsp if (error)
378 7d69d862 2021-11-15 stsp goto done;
379 20662ea0 2021-04-10 stsp }
380 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
381 0ae84acc 2022-06-15 tracey if (error != NULL)
382 0ae84acc 2022-06-15 tracey goto done;
383 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
384 20662ea0 2021-04-10 stsp if (error)
385 20662ea0 2021-04-10 stsp goto done;
386 57160834 2022-05-31 stsp #ifndef PROFILE
387 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
388 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
389 57160834 2022-05-31 stsp NULL) == -1)
390 57160834 2022-05-31 stsp err(1, "pledge");
391 57160834 2022-05-31 stsp #endif
392 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
393 20662ea0 2021-04-10 stsp if (error)
394 20662ea0 2021-04-10 stsp goto done;
395 20662ea0 2021-04-10 stsp
396 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
397 20662ea0 2021-04-10 stsp
398 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
399 20662ea0 2021-04-10 stsp if (gotconfig) {
400 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
401 20662ea0 2021-04-10 stsp int i, nremotes;
402 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
403 20662ea0 2021-04-10 stsp printf("default author: %s\n",
404 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
405 20662ea0 2021-04-10 stsp }
406 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
407 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
408 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
409 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
410 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
411 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
412 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
413 13b2084e 2021-09-06 stsp } else {
414 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
415 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
416 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
417 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
418 13b2084e 2021-09-06 stsp }
419 20662ea0 2021-04-10 stsp }
420 20662ea0 2021-04-10 stsp }
421 20662ea0 2021-04-10 stsp
422 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
423 20662ea0 2021-04-10 stsp &packsize, repo);
424 20662ea0 2021-04-10 stsp if (error)
425 20662ea0 2021-04-10 stsp goto done;
426 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
427 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
428 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
429 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
430 20662ea0 2021-04-10 stsp goto done;
431 20662ea0 2021-04-10 stsp }
432 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
433 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
434 20662ea0 2021-04-10 stsp }
435 20662ea0 2021-04-10 stsp
436 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
437 20662ea0 2021-04-10 stsp if (error)
438 20662ea0 2021-04-10 stsp goto done;
439 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
440 20662ea0 2021-04-10 stsp if (nobj > 0) {
441 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
442 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
443 20662ea0 2021-04-10 stsp goto done;
444 20662ea0 2021-04-10 stsp }
445 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
446 20662ea0 2021-04-10 stsp }
447 20662ea0 2021-04-10 stsp done:
448 20662ea0 2021-04-10 stsp if (repo)
449 20662ea0 2021-04-10 stsp got_repo_close(repo);
450 0ae84acc 2022-06-15 tracey if (pack_fds) {
451 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
452 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
453 0ae84acc 2022-06-15 tracey if (error == NULL)
454 0ae84acc 2022-06-15 tracey error = pack_err;
455 0ae84acc 2022-06-15 tracey }
456 0ae84acc 2022-06-15 tracey
457 7d69d862 2021-11-15 stsp free(repo_path);
458 20662ea0 2021-04-10 stsp return error;
459 05118f5a 2021-06-22 stsp }
460 05118f5a 2021-06-22 stsp
461 05118f5a 2021-06-22 stsp __dead static void
462 05118f5a 2021-06-22 stsp usage_pack(void)
463 05118f5a 2021-06-22 stsp {
464 c7a4fcc8 2023-02-17 op fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
465 827a167b 2022-08-16 stsp "[-x reference] [reference ...]\n", getprogname());
466 05118f5a 2021-06-22 stsp exit(1);
467 05118f5a 2021-06-22 stsp }
468 05118f5a 2021-06-22 stsp
469 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
470 2df845d5 2023-07-07 op FILE *out;
471 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
472 b8af7c06 2022-03-15 stsp int last_ncolored;
473 b8af7c06 2022-03-15 stsp int last_nfound;
474 b8af7c06 2022-03-15 stsp int last_ntrees;
475 b8af7c06 2022-03-15 stsp int loading_done;
476 05118f5a 2021-06-22 stsp int last_ncommits;
477 05118f5a 2021-06-22 stsp int last_nobj_total;
478 05118f5a 2021-06-22 stsp int last_p_deltify;
479 05118f5a 2021-06-22 stsp int last_p_written;
480 05118f5a 2021-06-22 stsp int last_p_indexed;
481 05118f5a 2021-06-22 stsp int last_p_resolved;
482 05118f5a 2021-06-22 stsp int verbosity;
483 05118f5a 2021-06-22 stsp int printed_something;
484 05118f5a 2021-06-22 stsp };
485 05118f5a 2021-06-22 stsp
486 b8af7c06 2022-03-15 stsp static void
487 2df845d5 2023-07-07 op print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
488 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees)
489 b8af7c06 2022-03-15 stsp {
490 b8af7c06 2022-03-15 stsp if (print_colored) {
491 2df845d5 2023-07-07 op fprintf(out, "%d commit%s colored", ncolored,
492 b8af7c06 2022-03-15 stsp ncolored == 1 ? "" : "s");
493 b8af7c06 2022-03-15 stsp }
494 b8af7c06 2022-03-15 stsp if (print_found) {
495 2df845d5 2023-07-07 op fprintf(out, "%s%d object%s found",
496 b8af7c06 2022-03-15 stsp ncolored > 0 ? "; " : "",
497 b8af7c06 2022-03-15 stsp nfound, nfound == 1 ? "" : "s");
498 b8af7c06 2022-03-15 stsp }
499 b8af7c06 2022-03-15 stsp if (print_trees) {
500 2df845d5 2023-07-07 op fprintf(out, "; %d tree%s scanned", ntrees,
501 b8af7c06 2022-03-15 stsp ntrees == 1 ? "" : "s");
502 b8af7c06 2022-03-15 stsp }
503 b8af7c06 2022-03-15 stsp }
504 b8af7c06 2022-03-15 stsp
505 05118f5a 2021-06-22 stsp static const struct got_error *
506 b8af7c06 2022-03-15 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
507 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
508 b8af7c06 2022-03-15 stsp int nobj_written)
509 05118f5a 2021-06-22 stsp {
510 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
511 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
512 05118f5a 2021-06-22 stsp int p_deltify, p_written;
513 b8af7c06 2022-03-15 stsp int print_colored = 0, print_found = 0, print_trees = 0;
514 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
515 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
516 05118f5a 2021-06-22 stsp
517 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
518 b8af7c06 2022-03-15 stsp return NULL;
519 b8af7c06 2022-03-15 stsp
520 b8af7c06 2022-03-15 stsp if (a->last_ncolored != ncolored) {
521 b8af7c06 2022-03-15 stsp print_colored = 1;
522 b8af7c06 2022-03-15 stsp a->last_ncolored = ncolored;
523 b8af7c06 2022-03-15 stsp }
524 b8af7c06 2022-03-15 stsp
525 b8af7c06 2022-03-15 stsp if (a->last_nfound != nfound) {
526 b8af7c06 2022-03-15 stsp print_colored = 1;
527 b8af7c06 2022-03-15 stsp print_found = 1;
528 b8af7c06 2022-03-15 stsp a->last_nfound = nfound;
529 b8af7c06 2022-03-15 stsp }
530 b8af7c06 2022-03-15 stsp
531 b8af7c06 2022-03-15 stsp if (a->last_ntrees != ntrees) {
532 b8af7c06 2022-03-15 stsp print_colored = 1;
533 b8af7c06 2022-03-15 stsp print_found = 1;
534 b8af7c06 2022-03-15 stsp print_trees = 1;
535 b8af7c06 2022-03-15 stsp a->last_ntrees = ntrees;
536 b8af7c06 2022-03-15 stsp }
537 b8af7c06 2022-03-15 stsp
538 b8af7c06 2022-03-15 stsp if ((print_colored || print_found || print_trees) &&
539 b8af7c06 2022-03-15 stsp !a->loading_done) {
540 2df845d5 2023-07-07 op fprintf(a->out, "\r");
541 2df845d5 2023-07-07 op print_load_info(a->out, print_colored, print_found,
542 2df845d5 2023-07-07 op print_trees, ncolored, nfound, ntrees);
543 b8af7c06 2022-03-15 stsp a->printed_something = 1;
544 2df845d5 2023-07-07 op fflush(a->out);
545 05118f5a 2021-06-22 stsp return NULL;
546 b8af7c06 2022-03-15 stsp } else if (!a->loading_done) {
547 2df845d5 2023-07-07 op fprintf(a->out, "\r");
548 2df845d5 2023-07-07 op print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
549 2df845d5 2023-07-07 op fprintf(a->out, "\n");
550 b8af7c06 2022-03-15 stsp a->loading_done = 1;
551 b8af7c06 2022-03-15 stsp }
552 05118f5a 2021-06-22 stsp
553 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
554 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
555 05118f5a 2021-06-22 stsp
556 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
557 05118f5a 2021-06-22 stsp print_searching = 1;
558 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
559 05118f5a 2021-06-22 stsp }
560 05118f5a 2021-06-22 stsp
561 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
562 05118f5a 2021-06-22 stsp print_searching = 1;
563 05118f5a 2021-06-22 stsp print_total = 1;
564 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
565 05118f5a 2021-06-22 stsp }
566 05118f5a 2021-06-22 stsp
567 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
568 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
569 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
570 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
571 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
572 05118f5a 2021-06-22 stsp }
573 05118f5a 2021-06-22 stsp
574 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
575 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
576 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
577 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
578 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
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 }
583 05118f5a 2021-06-22 stsp }
584 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
585 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
586 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
587 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
588 05118f5a 2021-06-22 stsp print_searching = 1;
589 05118f5a 2021-06-22 stsp print_total = 1;
590 05118f5a 2021-06-22 stsp print_deltify = 1;
591 05118f5a 2021-06-22 stsp print_written = 1;
592 05118f5a 2021-06-22 stsp }
593 05118f5a 2021-06-22 stsp }
594 05118f5a 2021-06-22 stsp }
595 05118f5a 2021-06-22 stsp
596 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
597 2df845d5 2023-07-07 op fprintf(a->out, "\r");
598 05118f5a 2021-06-22 stsp if (print_searching)
599 2df845d5 2023-07-07 op fprintf(a->out, "packing %d reference%s", ncommits,
600 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
601 05118f5a 2021-06-22 stsp if (print_total)
602 2df845d5 2023-07-07 op fprintf(a->out, "; %d object%s", nobj_total,
603 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
604 05118f5a 2021-06-22 stsp if (print_deltify)
605 2df845d5 2023-07-07 op fprintf(a->out, "; deltify: %d%%", p_deltify);
606 05118f5a 2021-06-22 stsp if (print_written)
607 2df845d5 2023-07-07 op fprintf(a->out, "; writing pack: %*s %d%%",
608 2df845d5 2023-07-07 op FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
609 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
610 05118f5a 2021-06-22 stsp print_written) {
611 05118f5a 2021-06-22 stsp a->printed_something = 1;
612 2df845d5 2023-07-07 op fflush(a->out);
613 05118f5a 2021-06-22 stsp }
614 05118f5a 2021-06-22 stsp return NULL;
615 05118f5a 2021-06-22 stsp }
616 05118f5a 2021-06-22 stsp
617 05118f5a 2021-06-22 stsp static const struct got_error *
618 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
619 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
620 05118f5a 2021-06-22 stsp {
621 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
622 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
623 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
624 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
625 05118f5a 2021-06-22 stsp
626 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
627 05118f5a 2021-06-22 stsp return NULL;
628 05118f5a 2021-06-22 stsp
629 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
630 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
631 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
632 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
633 05118f5a 2021-06-22 stsp print_size = 1;
634 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
635 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
636 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
637 05118f5a 2021-06-22 stsp }
638 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
639 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
640 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
641 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
642 05118f5a 2021-06-22 stsp print_indexed = 1;
643 05118f5a 2021-06-22 stsp print_size = 1;
644 05118f5a 2021-06-22 stsp }
645 05118f5a 2021-06-22 stsp }
646 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
647 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
648 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
649 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
650 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
651 05118f5a 2021-06-22 stsp print_resolved = 1;
652 05118f5a 2021-06-22 stsp print_indexed = 1;
653 05118f5a 2021-06-22 stsp print_size = 1;
654 05118f5a 2021-06-22 stsp }
655 05118f5a 2021-06-22 stsp }
656 05118f5a 2021-06-22 stsp
657 05118f5a 2021-06-22 stsp }
658 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
659 05118f5a 2021-06-22 stsp printf("\r");
660 05118f5a 2021-06-22 stsp if (print_size)
661 b5934965 2022-02-12 naddy printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
662 05118f5a 2021-06-22 stsp if (print_indexed)
663 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
664 05118f5a 2021-06-22 stsp if (print_resolved)
665 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
666 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
667 05118f5a 2021-06-22 stsp fflush(stdout);
668 05118f5a 2021-06-22 stsp
669 05118f5a 2021-06-22 stsp return NULL;
670 20662ea0 2021-04-10 stsp }
671 05118f5a 2021-06-22 stsp
672 05118f5a 2021-06-22 stsp static const struct got_error *
673 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
674 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
675 05118f5a 2021-06-22 stsp {
676 05118f5a 2021-06-22 stsp const struct got_error *err;
677 05118f5a 2021-06-22 stsp struct got_reference *ref;
678 05118f5a 2021-06-22 stsp
679 05118f5a 2021-06-22 stsp *new = NULL;
680 05118f5a 2021-06-22 stsp
681 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
682 05118f5a 2021-06-22 stsp if (err) {
683 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
684 05118f5a 2021-06-22 stsp return err;
685 05118f5a 2021-06-22 stsp
686 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
687 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
688 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
689 05118f5a 2021-06-22 stsp } else {
690 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
691 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
692 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
693 05118f5a 2021-06-22 stsp got_ref_close(ref);
694 05118f5a 2021-06-22 stsp }
695 05118f5a 2021-06-22 stsp
696 05118f5a 2021-06-22 stsp return err;
697 05118f5a 2021-06-22 stsp }
698 05118f5a 2021-06-22 stsp
699 05118f5a 2021-06-22 stsp static const struct got_error *
700 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
701 05118f5a 2021-06-22 stsp {
702 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
703 7d69d862 2021-11-15 stsp char *repo_path = NULL;
704 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
705 c7a4fcc8 2023-02-17 op int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
706 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
707 05118f5a 2021-06-22 stsp char *id_str = NULL;
708 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
709 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
710 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
711 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
712 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
713 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
714 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
715 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
716 05118f5a 2021-06-22 stsp
717 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
718 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
719 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
720 05118f5a 2021-06-22 stsp
721 1fa0d17d 2023-02-13 op #ifndef PROFILE
722 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
723 1fa0d17d 2023-02-13 op NULL) == -1)
724 1fa0d17d 2023-02-13 op err(1, "pledge");
725 1fa0d17d 2023-02-13 op #endif
726 1fa0d17d 2023-02-13 op
727 c7a4fcc8 2023-02-17 op while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
728 05118f5a 2021-06-22 stsp switch (ch) {
729 05118f5a 2021-06-22 stsp case 'a':
730 05118f5a 2021-06-22 stsp loose_obj_only = 0;
731 05118f5a 2021-06-22 stsp break;
732 c7a4fcc8 2023-02-17 op case 'D':
733 c7a4fcc8 2023-02-17 op force_refdelta = 1;
734 c7a4fcc8 2023-02-17 op break;
735 6f319063 2022-10-27 stsp case 'q':
736 6f319063 2022-10-27 stsp verbosity = -1;
737 6f319063 2022-10-27 stsp break;
738 05118f5a 2021-06-22 stsp case 'r':
739 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
740 05118f5a 2021-06-22 stsp if (repo_path == NULL)
741 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
742 05118f5a 2021-06-22 stsp optarg);
743 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
744 05118f5a 2021-06-22 stsp break;
745 05118f5a 2021-06-22 stsp case 'x':
746 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
747 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
748 05118f5a 2021-06-22 stsp optarg, NULL);
749 05118f5a 2021-06-22 stsp if (error)
750 05118f5a 2021-06-22 stsp return error;
751 20e420c8 2022-04-11 stsp break;
752 05118f5a 2021-06-22 stsp default:
753 05118f5a 2021-06-22 stsp usage_pack();
754 05118f5a 2021-06-22 stsp /* NOTREACHED */
755 05118f5a 2021-06-22 stsp }
756 05118f5a 2021-06-22 stsp }
757 05118f5a 2021-06-22 stsp
758 05118f5a 2021-06-22 stsp argc -= optind;
759 05118f5a 2021-06-22 stsp argv += optind;
760 05118f5a 2021-06-22 stsp
761 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
762 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
763 7d69d862 2021-11-15 stsp if (error)
764 7d69d862 2021-11-15 stsp goto done;
765 05118f5a 2021-06-22 stsp }
766 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
767 0ae84acc 2022-06-15 tracey if (error != NULL)
768 0ae84acc 2022-06-15 tracey goto done;
769 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
770 05118f5a 2021-06-22 stsp if (error)
771 05118f5a 2021-06-22 stsp goto done;
772 05118f5a 2021-06-22 stsp
773 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
774 05118f5a 2021-06-22 stsp if (error)
775 05118f5a 2021-06-22 stsp goto done;
776 05118f5a 2021-06-22 stsp
777 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
778 05118f5a 2021-06-22 stsp const char *refname = pe->path;
779 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
780 05118f5a 2021-06-22 stsp if (error)
781 05118f5a 2021-06-22 stsp goto done;
782 05118f5a 2021-06-22 stsp }
783 05118f5a 2021-06-22 stsp
784 05118f5a 2021-06-22 stsp if (argc == 0) {
785 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
786 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
787 05118f5a 2021-06-22 stsp if (error)
788 05118f5a 2021-06-22 stsp goto done;
789 05118f5a 2021-06-22 stsp } else {
790 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
791 05118f5a 2021-06-22 stsp const char *refname;
792 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
793 05118f5a 2021-06-22 stsp refname = argv[i];
794 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
795 05118f5a 2021-06-22 stsp if (error)
796 05118f5a 2021-06-22 stsp goto done;
797 05118f5a 2021-06-22 stsp }
798 05118f5a 2021-06-22 stsp }
799 05118f5a 2021-06-22 stsp
800 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
801 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
802 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
803 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
804 05118f5a 2021-06-22 stsp continue;
805 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
806 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
807 05118f5a 2021-06-22 stsp free(re);
808 05118f5a 2021-06-22 stsp }
809 05118f5a 2021-06-22 stsp
810 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
811 2df845d5 2023-07-07 op ppa.out = stdout;
812 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
813 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
814 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
815 20e420c8 2022-04-11 stsp ppa.verbosity = verbosity;
816 05118f5a 2021-06-22 stsp
817 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
818 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
819 c7a4fcc8 2023-02-17 op force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
820 05118f5a 2021-06-22 stsp if (error) {
821 05118f5a 2021-06-22 stsp if (ppa.printed_something)
822 05118f5a 2021-06-22 stsp printf("\n");
823 05118f5a 2021-06-22 stsp goto done;
824 05118f5a 2021-06-22 stsp }
825 05118f5a 2021-06-22 stsp
826 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
827 05118f5a 2021-06-22 stsp if (error)
828 05118f5a 2021-06-22 stsp goto done;
829 20e420c8 2022-04-11 stsp if (verbosity >= 0)
830 20e420c8 2022-04-11 stsp printf("\nWrote %s.pack\n", id_str);
831 05118f5a 2021-06-22 stsp
832 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
833 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
834 05118f5a 2021-06-22 stsp if (error)
835 05118f5a 2021-06-22 stsp goto done;
836 20e420c8 2022-04-11 stsp if (verbosity >= 0)
837 20e420c8 2022-04-11 stsp printf("\nIndexed %s.pack\n", id_str);
838 05118f5a 2021-06-22 stsp done:
839 f8eebdd4 2021-10-15 stsp if (repo)
840 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
841 0ae84acc 2022-06-15 tracey if (pack_fds) {
842 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
843 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
844 0ae84acc 2022-06-15 tracey if (error == NULL)
845 0ae84acc 2022-06-15 tracey error = pack_err;
846 0ae84acc 2022-06-15 tracey }
847 d8bacb93 2023-01-10 mark got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
848 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
849 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
850 05118f5a 2021-06-22 stsp free(id_str);
851 05118f5a 2021-06-22 stsp free(pack_hash);
852 7d69d862 2021-11-15 stsp free(repo_path);
853 05118f5a 2021-06-22 stsp return error;
854 05118f5a 2021-06-22 stsp }
855 05118f5a 2021-06-22 stsp
856 05118f5a 2021-06-22 stsp __dead static void
857 05118f5a 2021-06-22 stsp usage_indexpack(void)
858 05118f5a 2021-06-22 stsp {
859 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
860 05118f5a 2021-06-22 stsp getprogname());
861 05118f5a 2021-06-22 stsp exit(1);
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_indexpack(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 got_pack_progress_arg ppa;
874 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
875 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
876 05118f5a 2021-06-22 stsp
877 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
878 05118f5a 2021-06-22 stsp switch (ch) {
879 05118f5a 2021-06-22 stsp default:
880 05118f5a 2021-06-22 stsp usage_indexpack();
881 05118f5a 2021-06-22 stsp /* NOTREACHED */
882 05118f5a 2021-06-22 stsp }
883 05118f5a 2021-06-22 stsp }
884 05118f5a 2021-06-22 stsp
885 05118f5a 2021-06-22 stsp argc -= optind;
886 05118f5a 2021-06-22 stsp argv += optind;
887 05118f5a 2021-06-22 stsp
888 05118f5a 2021-06-22 stsp if (argc != 1)
889 05118f5a 2021-06-22 stsp usage_indexpack();
890 05118f5a 2021-06-22 stsp
891 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
892 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
893 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
894 05118f5a 2021-06-22 stsp
895 05118f5a 2021-06-22 stsp #ifndef PROFILE
896 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
897 05118f5a 2021-06-22 stsp NULL) == -1)
898 05118f5a 2021-06-22 stsp err(1, "pledge");
899 05118f5a 2021-06-22 stsp #endif
900 05118f5a 2021-06-22 stsp
901 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
902 0ae84acc 2022-06-15 tracey if (error != NULL)
903 0ae84acc 2022-06-15 tracey goto done;
904 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
905 05118f5a 2021-06-22 stsp if (error)
906 05118f5a 2021-06-22 stsp goto done;
907 05118f5a 2021-06-22 stsp
908 802c0f04 2021-10-15 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
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 memset(&ppa, 0, sizeof(ppa));
913 2df845d5 2023-07-07 op ppa.out = stdout;
914 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
915 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
916 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
917 05118f5a 2021-06-22 stsp
918 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
919 05118f5a 2021-06-22 stsp packfile_path);
920 05118f5a 2021-06-22 stsp if (error)
921 05118f5a 2021-06-22 stsp goto done;
922 05118f5a 2021-06-22 stsp
923 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
924 05118f5a 2021-06-22 stsp if (error)
925 05118f5a 2021-06-22 stsp goto done;
926 05118f5a 2021-06-22 stsp
927 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
928 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
929 05118f5a 2021-06-22 stsp if (error)
930 05118f5a 2021-06-22 stsp goto done;
931 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
932 05118f5a 2021-06-22 stsp done:
933 f8eebdd4 2021-10-15 stsp if (repo)
934 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
935 0ae84acc 2022-06-15 tracey if (pack_fds) {
936 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
937 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
938 0ae84acc 2022-06-15 tracey if (error == NULL)
939 0ae84acc 2022-06-15 tracey error = pack_err;
940 0ae84acc 2022-06-15 tracey }
941 05118f5a 2021-06-22 stsp free(id_str);
942 05118f5a 2021-06-22 stsp free(pack_hash);
943 05118f5a 2021-06-22 stsp return error;
944 05118f5a 2021-06-22 stsp }
945 05118f5a 2021-06-22 stsp
946 05118f5a 2021-06-22 stsp __dead static void
947 05118f5a 2021-06-22 stsp usage_listpack(void)
948 05118f5a 2021-06-22 stsp {
949 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
950 05118f5a 2021-06-22 stsp getprogname());
951 05118f5a 2021-06-22 stsp exit(1);
952 05118f5a 2021-06-22 stsp }
953 05118f5a 2021-06-22 stsp
954 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
955 05118f5a 2021-06-22 stsp int nblobs;
956 05118f5a 2021-06-22 stsp int ntrees;
957 05118f5a 2021-06-22 stsp int ncommits;
958 05118f5a 2021-06-22 stsp int ntags;
959 05118f5a 2021-06-22 stsp int noffdeltas;
960 05118f5a 2021-06-22 stsp int nrefdeltas;
961 05118f5a 2021-06-22 stsp int human_readable;
962 05118f5a 2021-06-22 stsp };
963 05118f5a 2021-06-22 stsp
964 05118f5a 2021-06-22 stsp static const struct got_error *
965 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
966 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
967 05118f5a 2021-06-22 stsp {
968 05118f5a 2021-06-22 stsp const struct got_error *err;
969 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
970 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
971 05118f5a 2021-06-22 stsp const char *type_str;
972 05118f5a 2021-06-22 stsp
973 0ae84acc 2022-06-15 tracey err = got_object_id_str(&id_str, id);
974 05118f5a 2021-06-22 stsp if (err)
975 05118f5a 2021-06-22 stsp return err;
976 05118f5a 2021-06-22 stsp
977 05118f5a 2021-06-22 stsp switch (type) {
978 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
979 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
980 05118f5a 2021-06-22 stsp a->nblobs++;
981 05118f5a 2021-06-22 stsp break;
982 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
983 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
984 05118f5a 2021-06-22 stsp a->ntrees++;
985 05118f5a 2021-06-22 stsp break;
986 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
987 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
988 05118f5a 2021-06-22 stsp a->ncommits++;
989 05118f5a 2021-06-22 stsp break;
990 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
991 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
992 05118f5a 2021-06-22 stsp a->ntags++;
993 05118f5a 2021-06-22 stsp break;
994 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
995 05118f5a 2021-06-22 stsp type_str = "offset-delta";
996 963ac08a 2021-09-25 naddy if (asprintf(&delta_str, " base-offset %lld",
997 963ac08a 2021-09-25 naddy (long long)base_offset) == -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->noffdeltas++;
1002 05118f5a 2021-06-22 stsp break;
1003 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
1004 05118f5a 2021-06-22 stsp type_str = "ref-delta";
1005 5e91dae4 2022-08-30 stsp err = got_object_id_str(&base_id_str, base_id);
1006 05118f5a 2021-06-22 stsp if (err)
1007 05118f5a 2021-06-22 stsp goto done;
1008 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1009 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
1010 05118f5a 2021-06-22 stsp goto done;
1011 05118f5a 2021-06-22 stsp }
1012 05118f5a 2021-06-22 stsp a->nrefdeltas++;
1013 05118f5a 2021-06-22 stsp break;
1014 05118f5a 2021-06-22 stsp default:
1015 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1016 05118f5a 2021-06-22 stsp goto done;
1017 05118f5a 2021-06-22 stsp }
1018 05118f5a 2021-06-22 stsp if (a->human_readable) {
1019 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
1020 05118f5a 2021-06-22 stsp char *s;;
1021 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
1022 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
1023 05118f5a 2021-06-22 stsp goto done;
1024 05118f5a 2021-06-22 stsp }
1025 05118f5a 2021-06-22 stsp s = scaled;
1026 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
1027 05118f5a 2021-06-22 stsp s++;
1028 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %s%s\n", id_str, type_str,
1029 963ac08a 2021-09-25 naddy (long long)offset, s, delta_str ? delta_str : "");
1030 05118f5a 2021-06-22 stsp } else {
1031 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1032 963ac08a 2021-09-25 naddy (long long)offset, (long long)size,
1033 963ac08a 2021-09-25 naddy delta_str ? delta_str : "");
1034 05118f5a 2021-06-22 stsp }
1035 05118f5a 2021-06-22 stsp done:
1036 05118f5a 2021-06-22 stsp free(id_str);
1037 05118f5a 2021-06-22 stsp free(base_id_str);
1038 05118f5a 2021-06-22 stsp free(delta_str);
1039 05118f5a 2021-06-22 stsp return err;
1040 05118f5a 2021-06-22 stsp }
1041 05118f5a 2021-06-22 stsp
1042 05118f5a 2021-06-22 stsp static const struct got_error *
1043 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
1044 05118f5a 2021-06-22 stsp {
1045 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
1046 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
1047 05118f5a 2021-06-22 stsp int ch;
1048 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
1049 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
1050 05118f5a 2021-06-22 stsp char *id_str = NULL;
1051 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
1052 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
1053 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
1054 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
1055 05118f5a 2021-06-22 stsp
1056 1fa0d17d 2023-02-13 op #ifndef PROFILE
1057 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1058 1fa0d17d 2023-02-13 op NULL) == -1)
1059 1fa0d17d 2023-02-13 op err(1, "pledge");
1060 1fa0d17d 2023-02-13 op #endif
1061 1fa0d17d 2023-02-13 op
1062 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
1063 05118f5a 2021-06-22 stsp switch (ch) {
1064 05118f5a 2021-06-22 stsp case 'h':
1065 05118f5a 2021-06-22 stsp human_readable = 1;
1066 05118f5a 2021-06-22 stsp break;
1067 05118f5a 2021-06-22 stsp case 's':
1068 05118f5a 2021-06-22 stsp show_stats = 1;
1069 05118f5a 2021-06-22 stsp break;
1070 05118f5a 2021-06-22 stsp default:
1071 05118f5a 2021-06-22 stsp usage_listpack();
1072 05118f5a 2021-06-22 stsp /* NOTREACHED */
1073 05118f5a 2021-06-22 stsp }
1074 05118f5a 2021-06-22 stsp }
1075 05118f5a 2021-06-22 stsp
1076 05118f5a 2021-06-22 stsp argc -= optind;
1077 05118f5a 2021-06-22 stsp argv += optind;
1078 05118f5a 2021-06-22 stsp
1079 05118f5a 2021-06-22 stsp if (argc != 1)
1080 05118f5a 2021-06-22 stsp usage_listpack();
1081 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
1082 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
1083 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
1084 05118f5a 2021-06-22 stsp
1085 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1086 0ae84acc 2022-06-15 tracey if (error != NULL)
1087 0ae84acc 2022-06-15 tracey goto done;
1088 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1089 05118f5a 2021-06-22 stsp if (error)
1090 05118f5a 2021-06-22 stsp goto done;
1091 57160834 2022-05-31 stsp #ifndef PROFILE
1092 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
1093 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1094 57160834 2022-05-31 stsp NULL) == -1)
1095 57160834 2022-05-31 stsp err(1, "pledge");
1096 57160834 2022-05-31 stsp #endif
1097 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1098 05118f5a 2021-06-22 stsp if (error)
1099 05118f5a 2021-06-22 stsp goto done;
1100 05118f5a 2021-06-22 stsp
1101 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
1102 05118f5a 2021-06-22 stsp packfile_path);
1103 05118f5a 2021-06-22 stsp if (error)
1104 05118f5a 2021-06-22 stsp goto done;
1105 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
1106 05118f5a 2021-06-22 stsp if (error)
1107 05118f5a 2021-06-22 stsp goto done;
1108 05118f5a 2021-06-22 stsp
1109 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
1110 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
1111 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
1112 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
1113 05118f5a 2021-06-22 stsp if (error)
1114 05118f5a 2021-06-22 stsp goto done;
1115 05118f5a 2021-06-22 stsp if (show_stats) {
1116 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1117 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1118 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1119 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
1120 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1121 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
1122 05118f5a 2021-06-22 stsp }
1123 05118f5a 2021-06-22 stsp done:
1124 f8eebdd4 2021-10-15 stsp if (repo)
1125 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
1126 0ae84acc 2022-06-15 tracey if (pack_fds) {
1127 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1128 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1129 0ae84acc 2022-06-15 tracey if (error == NULL)
1130 0ae84acc 2022-06-15 tracey error = pack_err;
1131 0ae84acc 2022-06-15 tracey }
1132 05118f5a 2021-06-22 stsp free(id_str);
1133 05118f5a 2021-06-22 stsp free(pack_hash);
1134 05118f5a 2021-06-22 stsp free(packfile_path);
1135 05118f5a 2021-06-22 stsp return error;
1136 b3d68e7f 2021-07-03 stsp }
1137 b3d68e7f 2021-07-03 stsp
1138 b3d68e7f 2021-07-03 stsp __dead static void
1139 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1140 b3d68e7f 2021-07-03 stsp {
1141 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1142 827a167b 2022-08-16 stsp getprogname());
1143 b3d68e7f 2021-07-03 stsp exit(1);
1144 b3d68e7f 2021-07-03 stsp }
1145 b3d68e7f 2021-07-03 stsp
1146 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1147 b3d68e7f 2021-07-03 stsp int last_nloose;
1148 b3d68e7f 2021-07-03 stsp int last_ncommits;
1149 b3d68e7f 2021-07-03 stsp int last_npurged;
1150 9a7c12cf 2023-06-18 op int last_nredundant;
1151 b3d68e7f 2021-07-03 stsp int verbosity;
1152 b3d68e7f 2021-07-03 stsp int printed_something;
1153 b3d68e7f 2021-07-03 stsp int dry_run;
1154 b3d68e7f 2021-07-03 stsp };
1155 b3d68e7f 2021-07-03 stsp
1156 b3d68e7f 2021-07-03 stsp static const struct got_error *
1157 0317ab6c 2023-07-11 op cleanup_progress(void *arg, int ncommits, int nloose, int npurged,
1158 9a7c12cf 2023-06-18 op int nredundant)
1159 b3d68e7f 2021-07-03 stsp {
1160 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1161 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1162 9a7c12cf 2023-06-18 op int print_redundant = 0;
1163 b3d68e7f 2021-07-03 stsp
1164 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1165 b3d68e7f 2021-07-03 stsp print_commits = 1;
1166 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1167 b3d68e7f 2021-07-03 stsp }
1168 0317ab6c 2023-07-11 op if (a->last_nloose != nloose) {
1169 0317ab6c 2023-07-11 op print_commits = 1;
1170 b3d68e7f 2021-07-03 stsp print_loose = 1;
1171 0317ab6c 2023-07-11 op a->last_nloose = nloose;
1172 0317ab6c 2023-07-11 op }
1173 0317ab6c 2023-07-11 op if (a->last_npurged != npurged) {
1174 b3d68e7f 2021-07-03 stsp print_commits = 1;
1175 0317ab6c 2023-07-11 op print_loose = 1;
1176 b3d68e7f 2021-07-03 stsp print_purged = 1;
1177 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1178 b3d68e7f 2021-07-03 stsp }
1179 9a7c12cf 2023-06-18 op if (a->last_nredundant != nredundant) {
1180 900499fd 2023-06-23 stsp print_commits = 1;
1181 0317ab6c 2023-07-11 op print_loose = 1;
1182 900499fd 2023-06-23 stsp print_purged = 1;
1183 9a7c12cf 2023-06-18 op print_redundant = 1;
1184 9a7c12cf 2023-06-18 op a->last_nredundant = nredundant;
1185 9a7c12cf 2023-06-18 op }
1186 b3d68e7f 2021-07-03 stsp
1187 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1188 b3d68e7f 2021-07-03 stsp return NULL;
1189 b3d68e7f 2021-07-03 stsp
1190 9a7c12cf 2023-06-18 op if (print_loose || print_commits || print_purged || print_redundant)
1191 b3d68e7f 2021-07-03 stsp printf("\r");
1192 b3d68e7f 2021-07-03 stsp if (print_commits)
1193 0317ab6c 2023-07-11 op printf("%d commit%s scanned", ncommits,
1194 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1195 0317ab6c 2023-07-11 op if (print_loose)
1196 0317ab6c 2023-07-11 op printf("; %d loose object%s", nloose, nloose == 1 ? "" : "s");
1197 900499fd 2023-06-23 stsp if (print_purged || print_redundant) {
1198 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1199 900499fd 2023-06-23 stsp printf("; could purge %d object%s", npurged,
1200 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1201 b3d68e7f 2021-07-03 stsp } else {
1202 900499fd 2023-06-23 stsp printf("; purged %d object%s", npurged,
1203 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1204 b3d68e7f 2021-07-03 stsp }
1205 b3d68e7f 2021-07-03 stsp }
1206 9a7c12cf 2023-06-18 op if (print_redundant) {
1207 9a7c12cf 2023-06-18 op if (a->dry_run) {
1208 900499fd 2023-06-23 stsp printf(", %d pack file%s", nredundant,
1209 9a7c12cf 2023-06-18 op nredundant == 1 ? "" : "s");
1210 9a7c12cf 2023-06-18 op } else {
1211 900499fd 2023-06-23 stsp printf(", %d pack file%s", nredundant,
1212 9a7c12cf 2023-06-18 op nredundant == 1 ? "" : "s");
1213 9a7c12cf 2023-06-18 op }
1214 9a7c12cf 2023-06-18 op }
1215 9a7c12cf 2023-06-18 op if (print_loose || print_commits || print_purged || print_redundant) {
1216 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1217 b3d68e7f 2021-07-03 stsp fflush(stdout);
1218 b3d68e7f 2021-07-03 stsp }
1219 b3d68e7f 2021-07-03 stsp return NULL;
1220 05118f5a 2021-06-22 stsp }
1221 b3d68e7f 2021-07-03 stsp
1222 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1223 1124fe40 2021-07-07 stsp int verbosity;
1224 1124fe40 2021-07-07 stsp int printed_something;
1225 1124fe40 2021-07-07 stsp int dry_run;
1226 1124fe40 2021-07-07 stsp };
1227 1124fe40 2021-07-07 stsp
1228 b3d68e7f 2021-07-03 stsp static const struct got_error *
1229 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1230 1124fe40 2021-07-07 stsp {
1231 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1232 1124fe40 2021-07-07 stsp
1233 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1234 1124fe40 2021-07-07 stsp return NULL;
1235 1124fe40 2021-07-07 stsp
1236 1124fe40 2021-07-07 stsp if (a->dry_run)
1237 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1238 1124fe40 2021-07-07 stsp else
1239 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1240 1124fe40 2021-07-07 stsp
1241 1124fe40 2021-07-07 stsp a->printed_something = 1;
1242 1124fe40 2021-07-07 stsp return NULL;
1243 1124fe40 2021-07-07 stsp }
1244 1124fe40 2021-07-07 stsp
1245 1124fe40 2021-07-07 stsp static const struct got_error *
1246 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1247 b3d68e7f 2021-07-03 stsp {
1248 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1249 7d69d862 2021-11-15 stsp char *repo_path = NULL;
1250 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1251 900499fd 2023-06-23 stsp int ch, dry_run = 0, verbosity = 0;
1252 900499fd 2023-06-23 stsp int ncommits = 0, nloose = 0, npacked = 0;
1253 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1254 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1255 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1256 9a7c12cf 2023-06-18 op off_t loose_before, loose_after;
1257 9a7c12cf 2023-06-18 op off_t pack_before, pack_after;
1258 9a7c12cf 2023-06-18 op off_t total_size;
1259 9a7c12cf 2023-06-18 op char loose_before_scaled[FMT_SCALED_STRSIZE];
1260 9a7c12cf 2023-06-18 op char loose_after_scaled[FMT_SCALED_STRSIZE];
1261 9a7c12cf 2023-06-18 op char pack_before_scaled[FMT_SCALED_STRSIZE];
1262 9a7c12cf 2023-06-18 op char pack_after_scaled[FMT_SCALED_STRSIZE];
1263 9a7c12cf 2023-06-18 op char total_size_scaled[FMT_SCALED_STRSIZE];
1264 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
1265 1fa0d17d 2023-02-13 op
1266 1fa0d17d 2023-02-13 op #ifndef PROFILE
1267 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1268 1fa0d17d 2023-02-13 op NULL) == -1)
1269 1fa0d17d 2023-02-13 op err(1, "pledge");
1270 1fa0d17d 2023-02-13 op #endif
1271 b3d68e7f 2021-07-03 stsp
1272 6f319063 2022-10-27 stsp while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1273 b3d68e7f 2021-07-03 stsp switch (ch) {
1274 ef8ec606 2021-07-27 stsp case 'a':
1275 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1276 ef8ec606 2021-07-27 stsp break;
1277 6f319063 2022-10-27 stsp case 'n':
1278 6f319063 2022-10-27 stsp dry_run = 1;
1279 6f319063 2022-10-27 stsp break;
1280 1124fe40 2021-07-07 stsp case 'p':
1281 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1282 1124fe40 2021-07-07 stsp break;
1283 6f319063 2022-10-27 stsp case 'q':
1284 6f319063 2022-10-27 stsp verbosity = -1;
1285 6f319063 2022-10-27 stsp break;
1286 b3d68e7f 2021-07-03 stsp case 'r':
1287 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1288 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1289 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1290 b3d68e7f 2021-07-03 stsp optarg);
1291 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1292 b3d68e7f 2021-07-03 stsp break;
1293 b3d68e7f 2021-07-03 stsp default:
1294 b3d68e7f 2021-07-03 stsp usage_cleanup();
1295 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1296 b3d68e7f 2021-07-03 stsp }
1297 b3d68e7f 2021-07-03 stsp }
1298 b3d68e7f 2021-07-03 stsp
1299 b3d68e7f 2021-07-03 stsp argc -= optind;
1300 b3d68e7f 2021-07-03 stsp argv += optind;
1301 b3d68e7f 2021-07-03 stsp
1302 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
1303 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
1304 7d69d862 2021-11-15 stsp if (error)
1305 7d69d862 2021-11-15 stsp goto done;
1306 b3d68e7f 2021-07-03 stsp }
1307 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1308 0ae84acc 2022-06-15 tracey if (error != NULL)
1309 0ae84acc 2022-06-15 tracey goto done;
1310 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1311 b3d68e7f 2021-07-03 stsp if (error)
1312 b3d68e7f 2021-07-03 stsp goto done;
1313 b3d68e7f 2021-07-03 stsp
1314 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1315 b3d68e7f 2021-07-03 stsp if (error)
1316 b3d68e7f 2021-07-03 stsp goto done;
1317 b3d68e7f 2021-07-03 stsp
1318 798586ca 2023-02-05 op if (got_repo_has_extension(repo, "preciousObjects")) {
1319 798586ca 2023-02-05 op error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1320 798586ca 2023-02-05 op "the preciousObjects Git extension is enabled; "
1321 798586ca 2023-02-05 op "this implies that objects must not be deleted");
1322 798586ca 2023-02-05 op goto done;
1323 9188bd78 2021-07-03 stsp }
1324 3bf0e21f 2023-06-19 op
1325 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1326 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1327 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1328 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1329 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1330 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1331 1124fe40 2021-07-07 stsp goto done;
1332 1124fe40 2021-07-07 stsp }
1333 1124fe40 2021-07-07 stsp
1334 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1335 0317ab6c 2023-07-11 op cpa.last_nloose = -1;
1336 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1337 9a7c12cf 2023-06-18 op cpa.last_nredundant = -1;
1338 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1339 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1340 9a7c12cf 2023-06-18 op
1341 0317ab6c 2023-07-11 op error = got_repo_cleanup(repo, &loose_before, &loose_after,
1342 0317ab6c 2023-07-11 op &pack_before, &pack_after, &ncommits, &nloose, &npacked,
1343 900499fd 2023-06-23 stsp dry_run, ignore_mtime, cleanup_progress, &cpa,
1344 900499fd 2023-06-23 stsp check_cancelled, NULL);
1345 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1346 b3d68e7f 2021-07-03 stsp printf("\n");
1347 9a7c12cf 2023-06-18 op if (error)
1348 9a7c12cf 2023-06-18 op goto done;
1349 9a7c12cf 2023-06-18 op
1350 9a7c12cf 2023-06-18 op total_size = (loose_before - loose_after) + (pack_before - pack_after);
1351 9a7c12cf 2023-06-18 op
1352 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1353 9a7c12cf 2023-06-18 op if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1354 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1355 b3d68e7f 2021-07-03 stsp goto done;
1356 b3d68e7f 2021-07-03 stsp }
1357 9a7c12cf 2023-06-18 op if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1358 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1359 b3d68e7f 2021-07-03 stsp goto done;
1360 b3d68e7f 2021-07-03 stsp }
1361 9a7c12cf 2023-06-18 op if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1362 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1363 b3d68e7f 2021-07-03 stsp goto done;
1364 b3d68e7f 2021-07-03 stsp }
1365 9a7c12cf 2023-06-18 op if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1366 9a7c12cf 2023-06-18 op error = got_error_from_errno("fmt_scaled");
1367 9a7c12cf 2023-06-18 op goto done;
1368 9a7c12cf 2023-06-18 op }
1369 9a7c12cf 2023-06-18 op if (fmt_scaled(total_size, total_size_scaled) == -1) {
1370 9a7c12cf 2023-06-18 op error = got_error_from_errno("fmt_scaled");
1371 9a7c12cf 2023-06-18 op goto done;
1372 9a7c12cf 2023-06-18 op }
1373 9a7c12cf 2023-06-18 op printf("loose total size before: %s\n", loose_before_scaled);
1374 9a7c12cf 2023-06-18 op printf("loose total size after: %s\n", loose_after_scaled);
1375 9a7c12cf 2023-06-18 op printf("pack files total size before: %s\n",
1376 9a7c12cf 2023-06-18 op pack_before_scaled);
1377 9a7c12cf 2023-06-18 op printf("pack files total size after: %s\n", pack_after_scaled);
1378 b3d68e7f 2021-07-03 stsp if (dry_run) {
1379 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1380 9a7c12cf 2023-06-18 op total_size_scaled);
1381 b3d68e7f 2021-07-03 stsp } else
1382 9a7c12cf 2023-06-18 op printf("disk space freed: %s\n", total_size_scaled);
1383 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1384 b3d68e7f 2021-07-03 stsp }
1385 9a7c12cf 2023-06-18 op
1386 b3d68e7f 2021-07-03 stsp done:
1387 b3d68e7f 2021-07-03 stsp if (repo)
1388 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1389 0ae84acc 2022-06-15 tracey if (pack_fds) {
1390 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1391 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1392 2df845d5 2023-07-07 op if (error == NULL)
1393 2df845d5 2023-07-07 op error = pack_err;
1394 2df845d5 2023-07-07 op }
1395 2df845d5 2023-07-07 op free(repo_path);
1396 2df845d5 2023-07-07 op return error;
1397 2df845d5 2023-07-07 op }
1398 2df845d5 2023-07-07 op
1399 2df845d5 2023-07-07 op __dead static void
1400 2df845d5 2023-07-07 op usage_dump(void)
1401 2df845d5 2023-07-07 op {
1402 2df845d5 2023-07-07 op fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1403 2df845d5 2023-07-07 op "[-x reference] [reference]...\n", getprogname());
1404 2df845d5 2023-07-07 op exit(1);
1405 2df845d5 2023-07-07 op }
1406 2df845d5 2023-07-07 op
1407 2df845d5 2023-07-07 op static const struct got_error *
1408 2df845d5 2023-07-07 op cmd_dump(int argc, char *argv[])
1409 2df845d5 2023-07-07 op {
1410 2df845d5 2023-07-07 op const struct got_error *error = NULL;
1411 2df845d5 2023-07-07 op struct got_pack_progress_arg ppa;
1412 2df845d5 2023-07-07 op struct got_repository *repo = NULL;
1413 2df845d5 2023-07-07 op struct got_pathlist_head exclude_args;
1414 2df845d5 2023-07-07 op struct got_pathlist_entry *pe;
1415 2df845d5 2023-07-07 op struct got_reflist_head exclude_refs;
1416 2df845d5 2023-07-07 op struct got_reflist_head include_refs;
1417 2df845d5 2023-07-07 op struct got_reflist_entry *re, *new;
1418 2df845d5 2023-07-07 op const char *refname;
1419 2df845d5 2023-07-07 op char *repo_path = NULL;
1420 2df845d5 2023-07-07 op int *pack_fds = NULL;
1421 2df845d5 2023-07-07 op int verbosity = 0;
1422 2df845d5 2023-07-07 op int i, ch;
1423 2df845d5 2023-07-07 op
1424 2df845d5 2023-07-07 op TAILQ_INIT(&exclude_args);
1425 2df845d5 2023-07-07 op TAILQ_INIT(&exclude_refs);
1426 2df845d5 2023-07-07 op TAILQ_INIT(&include_refs);
1427 2df845d5 2023-07-07 op
1428 2df845d5 2023-07-07 op #ifndef PROFILE
1429 2df845d5 2023-07-07 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1430 2df845d5 2023-07-07 op NULL) == -1)
1431 2df845d5 2023-07-07 op err(1, "pledge");
1432 2df845d5 2023-07-07 op #endif
1433 2df845d5 2023-07-07 op
1434 2df845d5 2023-07-07 op while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1435 2df845d5 2023-07-07 op switch (ch) {
1436 2df845d5 2023-07-07 op case 'q':
1437 2df845d5 2023-07-07 op verbosity = -1;
1438 2df845d5 2023-07-07 op break;
1439 2df845d5 2023-07-07 op case 'r':
1440 2df845d5 2023-07-07 op repo_path = realpath(optarg, NULL);
1441 2df845d5 2023-07-07 op if (repo_path == NULL)
1442 2df845d5 2023-07-07 op return got_error_from_errno2("realpath",
1443 2df845d5 2023-07-07 op optarg);
1444 2df845d5 2023-07-07 op got_path_strip_trailing_slashes(repo_path);
1445 2df845d5 2023-07-07 op break;
1446 2df845d5 2023-07-07 op case 'x':
1447 2df845d5 2023-07-07 op error = got_pathlist_append(&exclude_args,
1448 2df845d5 2023-07-07 op optarg, NULL);
1449 2df845d5 2023-07-07 op if (error)
1450 2df845d5 2023-07-07 op return error;
1451 2df845d5 2023-07-07 op break;
1452 2df845d5 2023-07-07 op default:
1453 2df845d5 2023-07-07 op usage_dump();
1454 2df845d5 2023-07-07 op /* NOTREACHED */
1455 2df845d5 2023-07-07 op }
1456 2df845d5 2023-07-07 op }
1457 2df845d5 2023-07-07 op argc -= optind;
1458 2df845d5 2023-07-07 op argv += optind;
1459 2df845d5 2023-07-07 op
1460 2df845d5 2023-07-07 op if (repo_path == NULL) {
1461 2df845d5 2023-07-07 op error = get_repo_path(&repo_path);
1462 2df845d5 2023-07-07 op if (error)
1463 2df845d5 2023-07-07 op goto done;
1464 2df845d5 2023-07-07 op }
1465 2df845d5 2023-07-07 op error = got_repo_pack_fds_open(&pack_fds);
1466 2df845d5 2023-07-07 op if (error != NULL)
1467 2df845d5 2023-07-07 op goto done;
1468 2df845d5 2023-07-07 op error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1469 2df845d5 2023-07-07 op if (error)
1470 2df845d5 2023-07-07 op goto done;
1471 2df845d5 2023-07-07 op
1472 2df845d5 2023-07-07 op error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1473 2df845d5 2023-07-07 op if (error)
1474 2df845d5 2023-07-07 op goto done;
1475 2df845d5 2023-07-07 op
1476 2df845d5 2023-07-07 op TAILQ_FOREACH(pe, &exclude_args, entry) {
1477 2df845d5 2023-07-07 op refname = pe->path;
1478 2df845d5 2023-07-07 op error = add_ref(&new, &exclude_refs, refname, repo);
1479 2df845d5 2023-07-07 op if (error)
1480 2df845d5 2023-07-07 op goto done;
1481 2df845d5 2023-07-07 op }
1482 2df845d5 2023-07-07 op
1483 2df845d5 2023-07-07 op if (argc == 0) {
1484 2df845d5 2023-07-07 op error = got_ref_list(&include_refs, repo, "",
1485 2df845d5 2023-07-07 op got_ref_cmp_by_name, NULL);
1486 2df845d5 2023-07-07 op if (error)
1487 2df845d5 2023-07-07 op goto done;
1488 2df845d5 2023-07-07 op } else {
1489 2df845d5 2023-07-07 op for (i = 0; i < argc; i++) {
1490 2df845d5 2023-07-07 op got_path_strip_trailing_slashes(argv[i]);
1491 2df845d5 2023-07-07 op refname = argv[i];
1492 2df845d5 2023-07-07 op error = add_ref(&new, &include_refs, refname, repo);
1493 2df845d5 2023-07-07 op if (error)
1494 2df845d5 2023-07-07 op goto done;
1495 2df845d5 2023-07-07 op }
1496 2df845d5 2023-07-07 op }
1497 2df845d5 2023-07-07 op
1498 2df845d5 2023-07-07 op /* Ignore references in the refs/got/ namespace. */
1499 2df845d5 2023-07-07 op TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1500 2df845d5 2023-07-07 op refname = got_ref_get_name(re->ref);
1501 2df845d5 2023-07-07 op if (strncmp("refs/got/", refname, 9) != 0)
1502 2df845d5 2023-07-07 op continue;
1503 2df845d5 2023-07-07 op TAILQ_REMOVE(&include_refs, re, entry);
1504 2df845d5 2023-07-07 op got_ref_close(re->ref);
1505 2df845d5 2023-07-07 op free(re);
1506 2df845d5 2023-07-07 op }
1507 2df845d5 2023-07-07 op
1508 2df845d5 2023-07-07 op memset(&ppa, 0, sizeof(ppa));
1509 2df845d5 2023-07-07 op ppa.out = stderr;
1510 2df845d5 2023-07-07 op ppa.verbosity = verbosity;
1511 2df845d5 2023-07-07 op
1512 2df845d5 2023-07-07 op error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1513 2df845d5 2023-07-07 op repo, pack_progress, &ppa, check_cancelled, NULL);
1514 2df845d5 2023-07-07 op if (ppa.printed_something)
1515 2df845d5 2023-07-07 op fprintf(stderr, "\n");
1516 2df845d5 2023-07-07 op done:
1517 2df845d5 2023-07-07 op if (repo)
1518 2df845d5 2023-07-07 op got_repo_close(repo);
1519 2df845d5 2023-07-07 op
1520 2df845d5 2023-07-07 op if (pack_fds) {
1521 2df845d5 2023-07-07 op const struct got_error *pack_err;
1522 2df845d5 2023-07-07 op
1523 2df845d5 2023-07-07 op pack_err = got_repo_pack_fds_close(pack_fds);
1524 0ae84acc 2022-06-15 tracey if (error == NULL)
1525 0ae84acc 2022-06-15 tracey error = pack_err;
1526 0ae84acc 2022-06-15 tracey }
1527 2df845d5 2023-07-07 op
1528 2df845d5 2023-07-07 op got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1529 2df845d5 2023-07-07 op got_ref_list_free(&exclude_refs);
1530 2df845d5 2023-07-07 op got_ref_list_free(&include_refs);
1531 f7d653fc 2023-07-09 op free(repo_path);
1532 f7d653fc 2023-07-09 op
1533 f7d653fc 2023-07-09 op return error;
1534 f7d653fc 2023-07-09 op }
1535 f7d653fc 2023-07-09 op
1536 f7d653fc 2023-07-09 op __dead static void
1537 f7d653fc 2023-07-09 op usage_load(void)
1538 f7d653fc 2023-07-09 op {
1539 02a7e21b 2023-07-10 stsp fprintf(stderr, "usage: %s load [-nq] [-l bundle-file] "
1540 02a7e21b 2023-07-10 stsp "[-r repository-path] [reference ...]\n",
1541 f7d653fc 2023-07-09 op getprogname());
1542 f7d653fc 2023-07-09 op exit(1);
1543 f7d653fc 2023-07-09 op }
1544 f7d653fc 2023-07-09 op
1545 f7d653fc 2023-07-09 op static const struct got_error *
1546 f7d653fc 2023-07-09 op load_progress(void *arg, off_t packfile_size, int nobj_total,
1547 f7d653fc 2023-07-09 op int nobj_indexed, int nobj_loose, int nobj_resolved)
1548 f7d653fc 2023-07-09 op {
1549 f7d653fc 2023-07-09 op return pack_index_progress(arg, packfile_size, nobj_total,
1550 f7d653fc 2023-07-09 op nobj_indexed, nobj_loose, nobj_resolved);
1551 f7d653fc 2023-07-09 op }
1552 f7d653fc 2023-07-09 op
1553 f7d653fc 2023-07-09 op static int
1554 f7d653fc 2023-07-09 op is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1555 f7d653fc 2023-07-09 op {
1556 f7d653fc 2023-07-09 op struct got_pathlist_entry *pe;
1557 f7d653fc 2023-07-09 op
1558 f7d653fc 2023-07-09 op if (TAILQ_EMPTY(wanted))
1559 f7d653fc 2023-07-09 op return 1;
1560 f7d653fc 2023-07-09 op
1561 f7d653fc 2023-07-09 op TAILQ_FOREACH(pe, wanted, entry) {
1562 f7d653fc 2023-07-09 op if (strcmp(pe->path, ref) == 0)
1563 f7d653fc 2023-07-09 op return 1;
1564 f7d653fc 2023-07-09 op }
1565 f7d653fc 2023-07-09 op
1566 f7d653fc 2023-07-09 op return 0;
1567 f7d653fc 2023-07-09 op }
1568 f7d653fc 2023-07-09 op
1569 f7d653fc 2023-07-09 op static const struct got_error *
1570 f7d653fc 2023-07-09 op create_ref(const char *refname, struct got_object_id *id,
1571 f7d653fc 2023-07-09 op int verbosity, struct got_repository *repo)
1572 f7d653fc 2023-07-09 op {
1573 f7d653fc 2023-07-09 op const struct got_error *err = NULL;
1574 f7d653fc 2023-07-09 op struct got_reference *ref;
1575 f7d653fc 2023-07-09 op char *id_str;
1576 f7d653fc 2023-07-09 op
1577 f7d653fc 2023-07-09 op err = got_object_id_str(&id_str, id);
1578 f7d653fc 2023-07-09 op if (err)
1579 f7d653fc 2023-07-09 op return err;
1580 f7d653fc 2023-07-09 op
1581 f7d653fc 2023-07-09 op err = got_ref_alloc(&ref, refname, id);
1582 f7d653fc 2023-07-09 op if (err)
1583 f7d653fc 2023-07-09 op goto done;
1584 f7d653fc 2023-07-09 op
1585 f7d653fc 2023-07-09 op err = got_ref_write(ref, repo);
1586 f7d653fc 2023-07-09 op got_ref_close(ref);
1587 f7d653fc 2023-07-09 op
1588 f7d653fc 2023-07-09 op if (err == NULL && verbosity >= 0)
1589 f7d653fc 2023-07-09 op printf("Created reference %s: %s\n", refname, id_str);
1590 f7d653fc 2023-07-09 op done:
1591 f7d653fc 2023-07-09 op free(id_str);
1592 f7d653fc 2023-07-09 op return err;
1593 f7d653fc 2023-07-09 op }
1594 f7d653fc 2023-07-09 op
1595 f7d653fc 2023-07-09 op static const struct got_error *
1596 f7d653fc 2023-07-09 op update_ref(struct got_reference *ref, struct got_object_id *new_id,
1597 f7d653fc 2023-07-09 op int replace_tags, int verbosity, struct got_repository *repo)
1598 f7d653fc 2023-07-09 op {
1599 f7d653fc 2023-07-09 op const struct got_error *err = NULL;
1600 f7d653fc 2023-07-09 op char *new_id_str = NULL;
1601 f7d653fc 2023-07-09 op struct got_object_id *old_id = NULL;
1602 f7d653fc 2023-07-09 op
1603 f7d653fc 2023-07-09 op err = got_object_id_str(&new_id_str, new_id);
1604 f7d653fc 2023-07-09 op if (err)
1605 f7d653fc 2023-07-09 op goto done;
1606 f7d653fc 2023-07-09 op
1607 f7d653fc 2023-07-09 op if (!replace_tags &&
1608 f7d653fc 2023-07-09 op strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1609 f7d653fc 2023-07-09 op err = got_ref_resolve(&old_id, repo, ref);
1610 f7d653fc 2023-07-09 op if (err)
1611 f7d653fc 2023-07-09 op goto done;
1612 f7d653fc 2023-07-09 op if (got_object_id_cmp(old_id, new_id) == 0)
1613 f7d653fc 2023-07-09 op goto done;
1614 f7d653fc 2023-07-09 op if (verbosity >= 0) {
1615 f7d653fc 2023-07-09 op printf("Rejecting update of existing tag %s: %s\n",
1616 f7d653fc 2023-07-09 op got_ref_get_name(ref), new_id_str);
1617 f7d653fc 2023-07-09 op }
1618 f7d653fc 2023-07-09 op goto done;
1619 f7d653fc 2023-07-09 op }
1620 f7d653fc 2023-07-09 op
1621 f7d653fc 2023-07-09 op if (got_ref_is_symbolic(ref)) {
1622 f7d653fc 2023-07-09 op if (verbosity >= 0) {
1623 f7d653fc 2023-07-09 op printf("Replacing reference %s: %s\n",
1624 f7d653fc 2023-07-09 op got_ref_get_name(ref),
1625 f7d653fc 2023-07-09 op got_ref_get_symref_target(ref));
1626 f7d653fc 2023-07-09 op }
1627 f7d653fc 2023-07-09 op err = got_ref_change_symref_to_ref(ref, new_id);
1628 f7d653fc 2023-07-09 op if (err)
1629 f7d653fc 2023-07-09 op goto done;
1630 f7d653fc 2023-07-09 op err = got_ref_write(ref, repo);
1631 f7d653fc 2023-07-09 op if (err)
1632 f7d653fc 2023-07-09 op goto done;
1633 f7d653fc 2023-07-09 op } else {
1634 f7d653fc 2023-07-09 op err = got_ref_resolve(&old_id, repo, ref);
1635 f7d653fc 2023-07-09 op if (err)
1636 f7d653fc 2023-07-09 op goto done;
1637 f7d653fc 2023-07-09 op if (got_object_id_cmp(old_id, new_id) == 0)
1638 f7d653fc 2023-07-09 op goto done;
1639 f7d653fc 2023-07-09 op
1640 f7d653fc 2023-07-09 op err = got_ref_change_ref(ref, new_id);
1641 f7d653fc 2023-07-09 op if (err)
1642 f7d653fc 2023-07-09 op goto done;
1643 f7d653fc 2023-07-09 op err = got_ref_write(ref, repo);
1644 f7d653fc 2023-07-09 op if (err)
1645 f7d653fc 2023-07-09 op goto done;
1646 f7d653fc 2023-07-09 op }
1647 f7d653fc 2023-07-09 op
1648 f7d653fc 2023-07-09 op if (verbosity >= 0)
1649 f7d653fc 2023-07-09 op printf("Updated %s: %s\n", got_ref_get_name(ref),
1650 f7d653fc 2023-07-09 op new_id_str);
1651 f7d653fc 2023-07-09 op done:
1652 f7d653fc 2023-07-09 op free(old_id);
1653 f7d653fc 2023-07-09 op free(new_id_str);
1654 f7d653fc 2023-07-09 op return err;
1655 f7d653fc 2023-07-09 op }
1656 f7d653fc 2023-07-09 op
1657 f7d653fc 2023-07-09 op static const struct got_error *
1658 f7d653fc 2023-07-09 op cmd_load(int argc, char *argv[])
1659 f7d653fc 2023-07-09 op {
1660 f7d653fc 2023-07-09 op const struct got_error *error = NULL;
1661 f7d653fc 2023-07-09 op struct got_repository *repo = NULL;
1662 f7d653fc 2023-07-09 op struct got_pathlist_head include_args;
1663 f7d653fc 2023-07-09 op struct got_pathlist_head available_refs;
1664 f7d653fc 2023-07-09 op struct got_pathlist_entry *pe;
1665 f7d653fc 2023-07-09 op struct got_pack_progress_arg ppa;
1666 f7d653fc 2023-07-09 op FILE *in = stdin;
1667 f7d653fc 2023-07-09 op int *pack_fds = NULL;
1668 f7d653fc 2023-07-09 op char *repo_path = NULL;
1669 f7d653fc 2023-07-09 op int list_refs_only = 0;
1670 f7d653fc 2023-07-09 op int noop = 0;
1671 f7d653fc 2023-07-09 op int verbosity = 0;
1672 02a7e21b 2023-07-10 stsp int ch, i;
1673 f7d653fc 2023-07-09 op
1674 f7d653fc 2023-07-09 op TAILQ_INIT(&include_args);
1675 f7d653fc 2023-07-09 op TAILQ_INIT(&available_refs);
1676 f7d653fc 2023-07-09 op
1677 f7d653fc 2023-07-09 op #ifndef PROFILE
1678 f7d653fc 2023-07-09 op if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1679 f7d653fc 2023-07-09 op "sendfd unveil", NULL) == -1)
1680 f7d653fc 2023-07-09 op err(1, "pledge");
1681 f7d653fc 2023-07-09 op #endif
1682 f7d653fc 2023-07-09 op
1683 02a7e21b 2023-07-10 stsp while ((ch = getopt(argc, argv, "l:nqr:")) != -1) {
1684 f7d653fc 2023-07-09 op switch (ch) {
1685 f7d653fc 2023-07-09 op case 'l':
1686 f7d653fc 2023-07-09 op list_refs_only = 1;
1687 02a7e21b 2023-07-10 stsp in = fopen(optarg, "re");
1688 02a7e21b 2023-07-10 stsp if (in == NULL)
1689 02a7e21b 2023-07-10 stsp return got_error_from_errno2("open", optarg);
1690 f7d653fc 2023-07-09 op break;
1691 f7d653fc 2023-07-09 op case 'n':
1692 f7d653fc 2023-07-09 op noop = 1;
1693 f7d653fc 2023-07-09 op break;
1694 f7d653fc 2023-07-09 op case 'q':
1695 f7d653fc 2023-07-09 op verbosity = -1;
1696 f7d653fc 2023-07-09 op break;
1697 f7d653fc 2023-07-09 op case 'r':
1698 f7d653fc 2023-07-09 op repo_path = realpath(optarg, NULL);
1699 f7d653fc 2023-07-09 op if (repo_path == NULL)
1700 f7d653fc 2023-07-09 op return got_error_from_errno2("realpath",
1701 f7d653fc 2023-07-09 op optarg);
1702 f7d653fc 2023-07-09 op got_path_strip_trailing_slashes(repo_path);
1703 f7d653fc 2023-07-09 op break;
1704 f7d653fc 2023-07-09 op default:
1705 f7d653fc 2023-07-09 op usage_load();
1706 f7d653fc 2023-07-09 op /* NOTREACHED */
1707 f7d653fc 2023-07-09 op }
1708 f7d653fc 2023-07-09 op }
1709 f7d653fc 2023-07-09 op argc -= optind;
1710 f7d653fc 2023-07-09 op argv += optind;
1711 f7d653fc 2023-07-09 op
1712 02a7e21b 2023-07-10 stsp if (list_refs_only && argc > 1)
1713 02a7e21b 2023-07-10 stsp errx(1, "-l and references on the command line are exclusive");
1714 f7d653fc 2023-07-09 op if (list_refs_only && noop)
1715 f7d653fc 2023-07-09 op errx(1, "-n and -l are mutually exclusive");
1716 f7d653fc 2023-07-09 op
1717 02a7e21b 2023-07-10 stsp for (i = 0; i < argc; i++) {
1718 02a7e21b 2023-07-10 stsp char *refname = argv[i];
1719 02a7e21b 2023-07-10 stsp got_path_strip_trailing_slashes(refname);
1720 02a7e21b 2023-07-10 stsp if (!got_ref_name_is_valid(refname))
1721 02a7e21b 2023-07-10 stsp errx(1, "invalid reference name %s", refname);
1722 02a7e21b 2023-07-10 stsp error = got_pathlist_append(&include_args, refname, NULL);
1723 02a7e21b 2023-07-10 stsp if (error)
1724 02a7e21b 2023-07-10 stsp goto done;
1725 f7d653fc 2023-07-09 op }
1726 f7d653fc 2023-07-09 op
1727 f7d653fc 2023-07-09 op if (repo_path == NULL) {
1728 f7d653fc 2023-07-09 op error = get_repo_path(&repo_path);
1729 f7d653fc 2023-07-09 op if (error)
1730 f7d653fc 2023-07-09 op goto done;
1731 f7d653fc 2023-07-09 op }
1732 f7d653fc 2023-07-09 op error = got_repo_pack_fds_open(&pack_fds);
1733 f7d653fc 2023-07-09 op if (error != NULL)
1734 f7d653fc 2023-07-09 op goto done;
1735 f7d653fc 2023-07-09 op error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1736 f7d653fc 2023-07-09 op if (error)
1737 f7d653fc 2023-07-09 op goto done;
1738 f7d653fc 2023-07-09 op
1739 f7d653fc 2023-07-09 op error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1740 f7d653fc 2023-07-09 op if (error)
1741 f7d653fc 2023-07-09 op goto done;
1742 f7d653fc 2023-07-09 op
1743 f7d653fc 2023-07-09 op memset(&ppa, 0, sizeof(ppa));
1744 f7d653fc 2023-07-09 op ppa.out = stdout;
1745 f7d653fc 2023-07-09 op ppa.verbosity = verbosity;
1746 f7d653fc 2023-07-09 op
1747 f7d653fc 2023-07-09 op error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1748 f7d653fc 2023-07-09 op load_progress, &ppa, check_cancelled, NULL);
1749 f254a62d 2023-07-09 op if (verbosity >= 0 && !list_refs_only)
1750 f7d653fc 2023-07-09 op printf("\n");
1751 f7d653fc 2023-07-09 op if (error)
1752 f7d653fc 2023-07-09 op goto done;
1753 f7d653fc 2023-07-09 op
1754 f7d653fc 2023-07-09 op if (list_refs_only) {
1755 f7d653fc 2023-07-09 op TAILQ_FOREACH(pe, &available_refs, entry) {
1756 f7d653fc 2023-07-09 op const char *refname = pe->path;
1757 f7d653fc 2023-07-09 op struct got_object_id *id = pe->data;
1758 f7d653fc 2023-07-09 op char *idstr;
1759 f7d653fc 2023-07-09 op
1760 f7d653fc 2023-07-09 op error = got_object_id_str(&idstr, id);
1761 f7d653fc 2023-07-09 op if (error)
1762 f7d653fc 2023-07-09 op goto done;
1763 f7d653fc 2023-07-09 op
1764 f7d653fc 2023-07-09 op printf("%s: %s\n", refname, idstr);
1765 f7d653fc 2023-07-09 op free(idstr);
1766 f7d653fc 2023-07-09 op }
1767 f7d653fc 2023-07-09 op goto done;
1768 f7d653fc 2023-07-09 op }
1769 f7d653fc 2023-07-09 op
1770 f7d653fc 2023-07-09 op if (noop)
1771 f7d653fc 2023-07-09 op goto done;
1772 f7d653fc 2023-07-09 op
1773 f7d653fc 2023-07-09 op /* Update references */
1774 f7d653fc 2023-07-09 op TAILQ_FOREACH(pe, &available_refs, entry) {
1775 f7d653fc 2023-07-09 op const struct got_error *unlock_err;
1776 f7d653fc 2023-07-09 op struct got_reference *ref;
1777 f7d653fc 2023-07-09 op const char *refname = pe->path;
1778 f7d653fc 2023-07-09 op struct got_object_id *id = pe->data;
1779 f7d653fc 2023-07-09 op
1780 f7d653fc 2023-07-09 op if (!is_wanted_ref(&include_args, pe->path))
1781 f7d653fc 2023-07-09 op continue;
1782 f7d653fc 2023-07-09 op
1783 f7d653fc 2023-07-09 op error = got_ref_open(&ref, repo, refname, 1);
1784 f7d653fc 2023-07-09 op if (error) {
1785 f7d653fc 2023-07-09 op if (error->code != GOT_ERR_NOT_REF)
1786 f7d653fc 2023-07-09 op goto done;
1787 f7d653fc 2023-07-09 op error = create_ref(refname, id, verbosity, repo);
1788 f7d653fc 2023-07-09 op if (error)
1789 f7d653fc 2023-07-09 op goto done;
1790 f7d653fc 2023-07-09 op } else {
1791 f7d653fc 2023-07-09 op /* XXX: check advances only and add -f to force? */
1792 f7d653fc 2023-07-09 op error = update_ref(ref, id, 1, verbosity, repo);
1793 f7d653fc 2023-07-09 op unlock_err = got_ref_unlock(ref);
1794 f7d653fc 2023-07-09 op if (unlock_err && error == NULL)
1795 f7d653fc 2023-07-09 op error = unlock_err;
1796 f7d653fc 2023-07-09 op got_ref_close(ref);
1797 f7d653fc 2023-07-09 op if (error)
1798 f7d653fc 2023-07-09 op goto done;
1799 f7d653fc 2023-07-09 op }
1800 f7d653fc 2023-07-09 op }
1801 f7d653fc 2023-07-09 op
1802 f7d653fc 2023-07-09 op done:
1803 f7d653fc 2023-07-09 op if (in != stdin && fclose(in) == EOF && error == NULL)
1804 f7d653fc 2023-07-09 op error = got_error_from_errno("fclose");
1805 f7d653fc 2023-07-09 op
1806 f7d653fc 2023-07-09 op if (repo)
1807 f7d653fc 2023-07-09 op got_repo_close(repo);
1808 f7d653fc 2023-07-09 op
1809 f7d653fc 2023-07-09 op if (pack_fds) {
1810 f7d653fc 2023-07-09 op const struct got_error *pack_err;
1811 f7d653fc 2023-07-09 op
1812 f7d653fc 2023-07-09 op pack_err = got_repo_pack_fds_close(pack_fds);
1813 f7d653fc 2023-07-09 op if (error == NULL)
1814 f7d653fc 2023-07-09 op error = pack_err;
1815 f7d653fc 2023-07-09 op }
1816 f7d653fc 2023-07-09 op
1817 f7d653fc 2023-07-09 op got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1818 f7d653fc 2023-07-09 op got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);
1819 7d69d862 2021-11-15 stsp free(repo_path);
1820 2df845d5 2023-07-07 op
1821 b3d68e7f 2021-07-03 stsp return error;
1822 b3d68e7f 2021-07-03 stsp }