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