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