Blame


1 795b88cb 2023-07-10 thomas /*
2 795b88cb 2023-07-10 thomas * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 795b88cb 2023-07-10 thomas * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 795b88cb 2023-07-10 thomas * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 795b88cb 2023-07-10 thomas * Copyright (C) 2023 Josh Rickmar <jrick@zettaport.com>
6 795b88cb 2023-07-10 thomas *
7 795b88cb 2023-07-10 thomas * Permission to use, copy, modify, and distribute this software for any
8 795b88cb 2023-07-10 thomas * purpose with or without fee is hereby granted, provided that the above
9 795b88cb 2023-07-10 thomas * copyright notice and this permission notice appear in all copies.
10 795b88cb 2023-07-10 thomas *
11 795b88cb 2023-07-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 795b88cb 2023-07-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 795b88cb 2023-07-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 795b88cb 2023-07-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 795b88cb 2023-07-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 795b88cb 2023-07-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 795b88cb 2023-07-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 795b88cb 2023-07-10 thomas */
19 795b88cb 2023-07-10 thomas
20 b7eff127 2023-07-10 thomas #include "got_compat.h"
21 b7eff127 2023-07-10 thomas
22 795b88cb 2023-07-10 thomas #include <sys/time.h>
23 795b88cb 2023-07-10 thomas #include <sys/types.h>
24 795b88cb 2023-07-10 thomas #include <sys/stat.h>
25 795b88cb 2023-07-10 thomas #include <sys/wait.h>
26 795b88cb 2023-07-10 thomas
27 795b88cb 2023-07-10 thomas #include <err.h>
28 795b88cb 2023-07-10 thomas #include <errno.h>
29 795b88cb 2023-07-10 thomas #include <fcntl.h>
30 795b88cb 2023-07-10 thomas #include <limits.h>
31 795b88cb 2023-07-10 thomas #include <locale.h>
32 795b88cb 2023-07-10 thomas #include <ctype.h>
33 795b88cb 2023-07-10 thomas #include <signal.h>
34 795b88cb 2023-07-10 thomas #include <stdio.h>
35 795b88cb 2023-07-10 thomas #include <stdlib.h>
36 795b88cb 2023-07-10 thomas #include <string.h>
37 795b88cb 2023-07-10 thomas #include <unistd.h>
38 795b88cb 2023-07-10 thomas #include <libgen.h>
39 795b88cb 2023-07-10 thomas #include <time.h>
40 795b88cb 2023-07-10 thomas #include <paths.h>
41 795b88cb 2023-07-10 thomas #include <regex.h>
42 795b88cb 2023-07-10 thomas #include <getopt.h>
43 795b88cb 2023-07-10 thomas
44 795b88cb 2023-07-10 thomas #include "got_version.h"
45 795b88cb 2023-07-10 thomas #include "got_error.h"
46 795b88cb 2023-07-10 thomas #include "got_object.h"
47 795b88cb 2023-07-10 thomas #include "got_reference.h"
48 795b88cb 2023-07-10 thomas #include "got_repository.h"
49 795b88cb 2023-07-10 thomas #include "got_path.h"
50 795b88cb 2023-07-10 thomas #include "got_cancel.h"
51 795b88cb 2023-07-10 thomas #include "got_worktree.h"
52 795b88cb 2023-07-10 thomas #include "got_worktree_cvg.h"
53 795b88cb 2023-07-10 thomas #include "got_diff.h"
54 795b88cb 2023-07-10 thomas #include "got_commit_graph.h"
55 795b88cb 2023-07-10 thomas #include "got_fetch.h"
56 795b88cb 2023-07-10 thomas #include "got_send.h"
57 795b88cb 2023-07-10 thomas #include "got_blame.h"
58 795b88cb 2023-07-10 thomas #include "got_privsep.h"
59 795b88cb 2023-07-10 thomas #include "got_opentemp.h"
60 795b88cb 2023-07-10 thomas #include "got_gotconfig.h"
61 795b88cb 2023-07-10 thomas #include "got_dial.h"
62 795b88cb 2023-07-10 thomas #include "got_patch.h"
63 795b88cb 2023-07-10 thomas #include "got_sigs.h"
64 795b88cb 2023-07-10 thomas #include "got_date.h"
65 795b88cb 2023-07-10 thomas
66 795b88cb 2023-07-10 thomas #ifndef nitems
67 795b88cb 2023-07-10 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 a8938c43 2024-03-19 thomas #endif
69 a8938c43 2024-03-19 thomas
70 a8938c43 2024-03-19 thomas #ifndef GOT_DEFAULT_EDITOR
71 a8938c43 2024-03-19 thomas #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
72 795b88cb 2023-07-10 thomas #endif
73 795b88cb 2023-07-10 thomas
74 795b88cb 2023-07-10 thomas static volatile sig_atomic_t sigint_received;
75 795b88cb 2023-07-10 thomas static volatile sig_atomic_t sigpipe_received;
76 795b88cb 2023-07-10 thomas
77 795b88cb 2023-07-10 thomas static void
78 795b88cb 2023-07-10 thomas catch_sigint(int signo)
79 795b88cb 2023-07-10 thomas {
80 795b88cb 2023-07-10 thomas sigint_received = 1;
81 795b88cb 2023-07-10 thomas }
82 795b88cb 2023-07-10 thomas
83 795b88cb 2023-07-10 thomas static void
84 795b88cb 2023-07-10 thomas catch_sigpipe(int signo)
85 795b88cb 2023-07-10 thomas {
86 795b88cb 2023-07-10 thomas sigpipe_received = 1;
87 795b88cb 2023-07-10 thomas }
88 795b88cb 2023-07-10 thomas
89 795b88cb 2023-07-10 thomas
90 795b88cb 2023-07-10 thomas struct got_cmd {
91 795b88cb 2023-07-10 thomas const char *cmd_name;
92 795b88cb 2023-07-10 thomas const struct got_error *(*cmd_main)(int, char *[]);
93 795b88cb 2023-07-10 thomas void (*cmd_usage)(void);
94 795b88cb 2023-07-10 thomas const char *cmd_alias;
95 795b88cb 2023-07-10 thomas };
96 795b88cb 2023-07-10 thomas
97 795b88cb 2023-07-10 thomas __dead static void usage(int, int);
98 795b88cb 2023-07-10 thomas __dead static void usage_import(void);
99 795b88cb 2023-07-10 thomas __dead static void usage_clone(void);
100 795b88cb 2023-07-10 thomas __dead static void usage_checkout(void);
101 795b88cb 2023-07-10 thomas __dead static void usage_update(void);
102 795b88cb 2023-07-10 thomas __dead static void usage_log(void);
103 795b88cb 2023-07-10 thomas __dead static void usage_diff(void);
104 795b88cb 2023-07-10 thomas __dead static void usage_blame(void);
105 795b88cb 2023-07-10 thomas __dead static void usage_tree(void);
106 795b88cb 2023-07-10 thomas __dead static void usage_status(void);
107 795b88cb 2023-07-10 thomas __dead static void usage_tag(void);
108 795b88cb 2023-07-10 thomas __dead static void usage_add(void);
109 795b88cb 2023-07-10 thomas __dead static void usage_remove(void);
110 795b88cb 2023-07-10 thomas __dead static void usage_patch(void);
111 795b88cb 2023-07-10 thomas __dead static void usage_revert(void);
112 795b88cb 2023-07-10 thomas __dead static void usage_commit(void);
113 795b88cb 2023-07-10 thomas __dead static void usage_cherrypick(void);
114 795b88cb 2023-07-10 thomas __dead static void usage_backout(void);
115 795b88cb 2023-07-10 thomas __dead static void usage_cat(void);
116 795b88cb 2023-07-10 thomas __dead static void usage_info(void);
117 795b88cb 2023-07-10 thomas
118 795b88cb 2023-07-10 thomas static const struct got_error* cmd_import(int, char *[]);
119 795b88cb 2023-07-10 thomas static const struct got_error* cmd_clone(int, char *[]);
120 795b88cb 2023-07-10 thomas static const struct got_error* cmd_checkout(int, char *[]);
121 795b88cb 2023-07-10 thomas static const struct got_error* cmd_update(int, char *[]);
122 795b88cb 2023-07-10 thomas static const struct got_error* cmd_log(int, char *[]);
123 795b88cb 2023-07-10 thomas static const struct got_error* cmd_diff(int, char *[]);
124 795b88cb 2023-07-10 thomas static const struct got_error* cmd_blame(int, char *[]);
125 795b88cb 2023-07-10 thomas static const struct got_error* cmd_tree(int, char *[]);
126 795b88cb 2023-07-10 thomas static const struct got_error* cmd_status(int, char *[]);
127 795b88cb 2023-07-10 thomas static const struct got_error* cmd_tag(int, char *[]);
128 795b88cb 2023-07-10 thomas static const struct got_error* cmd_add(int, char *[]);
129 795b88cb 2023-07-10 thomas static const struct got_error* cmd_remove(int, char *[]);
130 795b88cb 2023-07-10 thomas static const struct got_error* cmd_patch(int, char *[]);
131 795b88cb 2023-07-10 thomas static const struct got_error* cmd_revert(int, char *[]);
132 795b88cb 2023-07-10 thomas static const struct got_error* cmd_commit(int, char *[]);
133 795b88cb 2023-07-10 thomas static const struct got_error* cmd_cherrypick(int, char *[]);
134 795b88cb 2023-07-10 thomas static const struct got_error* cmd_backout(int, char *[]);
135 795b88cb 2023-07-10 thomas static const struct got_error* cmd_cat(int, char *[]);
136 795b88cb 2023-07-10 thomas static const struct got_error* cmd_info(int, char *[]);
137 795b88cb 2023-07-10 thomas
138 795b88cb 2023-07-10 thomas static const struct got_cmd got_commands[] = {
139 795b88cb 2023-07-10 thomas { "import", cmd_import, usage_import, "im" },
140 795b88cb 2023-07-10 thomas { "clone", cmd_clone, usage_clone, "cl" },
141 795b88cb 2023-07-10 thomas { "checkout", cmd_checkout, usage_checkout, "co" },
142 795b88cb 2023-07-10 thomas { "update", cmd_update, usage_update, "up" },
143 795b88cb 2023-07-10 thomas { "log", cmd_log, usage_log, "" },
144 795b88cb 2023-07-10 thomas { "diff", cmd_diff, usage_diff, "di" },
145 795b88cb 2023-07-10 thomas { "blame", cmd_blame, usage_blame, "bl" },
146 795b88cb 2023-07-10 thomas { "tree", cmd_tree, usage_tree, "tr" },
147 795b88cb 2023-07-10 thomas { "status", cmd_status, usage_status, "st" },
148 795b88cb 2023-07-10 thomas { "tag", cmd_tag, usage_tag, "" },
149 795b88cb 2023-07-10 thomas { "add", cmd_add, usage_add, "" },
150 795b88cb 2023-07-10 thomas { "remove", cmd_remove, usage_remove, "rm" },
151 795b88cb 2023-07-10 thomas { "patch", cmd_patch, usage_patch, "pa" },
152 795b88cb 2023-07-10 thomas { "revert", cmd_revert, usage_revert, "rv" },
153 795b88cb 2023-07-10 thomas { "commit", cmd_commit, usage_commit, "ci" },
154 795b88cb 2023-07-10 thomas { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
155 795b88cb 2023-07-10 thomas { "backout", cmd_backout, usage_backout, "bo" },
156 795b88cb 2023-07-10 thomas { "cat", cmd_cat, usage_cat, "" },
157 795b88cb 2023-07-10 thomas { "info", cmd_info, usage_info, "" },
158 795b88cb 2023-07-10 thomas };
159 795b88cb 2023-07-10 thomas
160 795b88cb 2023-07-10 thomas static void
161 795b88cb 2023-07-10 thomas list_commands(FILE *fp)
162 795b88cb 2023-07-10 thomas {
163 795b88cb 2023-07-10 thomas size_t i;
164 795b88cb 2023-07-10 thomas
165 795b88cb 2023-07-10 thomas fprintf(fp, "commands:");
166 795b88cb 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
167 795b88cb 2023-07-10 thomas const struct got_cmd *cmd = &got_commands[i];
168 795b88cb 2023-07-10 thomas fprintf(fp, " %s", cmd->cmd_name);
169 795b88cb 2023-07-10 thomas }
170 795b88cb 2023-07-10 thomas fputc('\n', fp);
171 795b88cb 2023-07-10 thomas }
172 795b88cb 2023-07-10 thomas
173 795b88cb 2023-07-10 thomas __dead static void
174 795b88cb 2023-07-10 thomas option_conflict(char a, char b)
175 795b88cb 2023-07-10 thomas {
176 795b88cb 2023-07-10 thomas errx(1, "-%c and -%c options are mutually exclusive", a, b);
177 795b88cb 2023-07-10 thomas }
178 795b88cb 2023-07-10 thomas
179 795b88cb 2023-07-10 thomas int
180 795b88cb 2023-07-10 thomas main(int argc, char *argv[])
181 795b88cb 2023-07-10 thomas {
182 795b88cb 2023-07-10 thomas const struct got_cmd *cmd;
183 795b88cb 2023-07-10 thomas size_t i;
184 795b88cb 2023-07-10 thomas int ch;
185 795b88cb 2023-07-10 thomas int hflag = 0, Vflag = 0;
186 795b88cb 2023-07-10 thomas static const struct option longopts[] = {
187 795b88cb 2023-07-10 thomas { "version", no_argument, NULL, 'V' },
188 795b88cb 2023-07-10 thomas { NULL, 0, NULL, 0 }
189 795b88cb 2023-07-10 thomas };
190 795b88cb 2023-07-10 thomas
191 795b88cb 2023-07-10 thomas setlocale(LC_CTYPE, "");
192 795b88cb 2023-07-10 thomas
193 795b88cb 2023-07-10 thomas while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
194 795b88cb 2023-07-10 thomas switch (ch) {
195 795b88cb 2023-07-10 thomas case 'h':
196 795b88cb 2023-07-10 thomas hflag = 1;
197 795b88cb 2023-07-10 thomas break;
198 795b88cb 2023-07-10 thomas case 'V':
199 795b88cb 2023-07-10 thomas Vflag = 1;
200 795b88cb 2023-07-10 thomas break;
201 795b88cb 2023-07-10 thomas default:
202 795b88cb 2023-07-10 thomas usage(hflag, 1);
203 795b88cb 2023-07-10 thomas /* NOTREACHED */
204 795b88cb 2023-07-10 thomas }
205 795b88cb 2023-07-10 thomas }
206 795b88cb 2023-07-10 thomas
207 795b88cb 2023-07-10 thomas argc -= optind;
208 795b88cb 2023-07-10 thomas argv += optind;
209 795b88cb 2023-07-10 thomas optind = 1;
210 795b88cb 2023-07-10 thomas optreset = 1;
211 795b88cb 2023-07-10 thomas
212 795b88cb 2023-07-10 thomas if (Vflag) {
213 795b88cb 2023-07-10 thomas got_version_print_str();
214 795b88cb 2023-07-10 thomas return 0;
215 795b88cb 2023-07-10 thomas }
216 795b88cb 2023-07-10 thomas
217 795b88cb 2023-07-10 thomas if (argc <= 0)
218 795b88cb 2023-07-10 thomas usage(hflag, hflag ? 0 : 1);
219 795b88cb 2023-07-10 thomas
220 795b88cb 2023-07-10 thomas signal(SIGINT, catch_sigint);
221 795b88cb 2023-07-10 thomas signal(SIGPIPE, catch_sigpipe);
222 795b88cb 2023-07-10 thomas
223 795b88cb 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
224 795b88cb 2023-07-10 thomas const struct got_error *error;
225 795b88cb 2023-07-10 thomas
226 795b88cb 2023-07-10 thomas cmd = &got_commands[i];
227 795b88cb 2023-07-10 thomas
228 795b88cb 2023-07-10 thomas if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
229 795b88cb 2023-07-10 thomas strcmp(cmd->cmd_alias, argv[0]) != 0)
230 795b88cb 2023-07-10 thomas continue;
231 795b88cb 2023-07-10 thomas
232 795b88cb 2023-07-10 thomas if (hflag)
233 795b88cb 2023-07-10 thomas cmd->cmd_usage();
234 795b88cb 2023-07-10 thomas
235 795b88cb 2023-07-10 thomas error = cmd->cmd_main(argc, argv);
236 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_CANCELLED &&
237 795b88cb 2023-07-10 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
238 795b88cb 2023-07-10 thomas !(sigpipe_received &&
239 795b88cb 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
240 795b88cb 2023-07-10 thomas !(sigint_received &&
241 795b88cb 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EINTR)) {
242 795b88cb 2023-07-10 thomas fflush(stdout);
243 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
244 795b88cb 2023-07-10 thomas return 1;
245 795b88cb 2023-07-10 thomas }
246 795b88cb 2023-07-10 thomas
247 795b88cb 2023-07-10 thomas return 0;
248 795b88cb 2023-07-10 thomas }
249 795b88cb 2023-07-10 thomas
250 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
251 795b88cb 2023-07-10 thomas list_commands(stderr);
252 795b88cb 2023-07-10 thomas return 1;
253 795b88cb 2023-07-10 thomas }
254 795b88cb 2023-07-10 thomas
255 795b88cb 2023-07-10 thomas __dead static void
256 795b88cb 2023-07-10 thomas usage(int hflag, int status)
257 795b88cb 2023-07-10 thomas {
258 795b88cb 2023-07-10 thomas FILE *fp = (status == 0) ? stdout : stderr;
259 795b88cb 2023-07-10 thomas
260 795b88cb 2023-07-10 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
261 795b88cb 2023-07-10 thomas getprogname());
262 795b88cb 2023-07-10 thomas if (hflag)
263 795b88cb 2023-07-10 thomas list_commands(fp);
264 795b88cb 2023-07-10 thomas exit(status);
265 795b88cb 2023-07-10 thomas }
266 795b88cb 2023-07-10 thomas
267 795b88cb 2023-07-10 thomas static const struct got_error *
268 795b88cb 2023-07-10 thomas get_editor(char **abspath)
269 795b88cb 2023-07-10 thomas {
270 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
271 795b88cb 2023-07-10 thomas const char *editor;
272 795b88cb 2023-07-10 thomas
273 795b88cb 2023-07-10 thomas *abspath = NULL;
274 795b88cb 2023-07-10 thomas
275 795b88cb 2023-07-10 thomas editor = getenv("VISUAL");
276 795b88cb 2023-07-10 thomas if (editor == NULL)
277 795b88cb 2023-07-10 thomas editor = getenv("EDITOR");
278 795b88cb 2023-07-10 thomas
279 795b88cb 2023-07-10 thomas if (editor) {
280 795b88cb 2023-07-10 thomas err = got_path_find_prog(abspath, editor);
281 795b88cb 2023-07-10 thomas if (err)
282 795b88cb 2023-07-10 thomas return err;
283 795b88cb 2023-07-10 thomas }
284 795b88cb 2023-07-10 thomas
285 795b88cb 2023-07-10 thomas if (*abspath == NULL) {
286 a8938c43 2024-03-19 thomas *abspath = strdup(GOT_DEFAULT_EDITOR);
287 795b88cb 2023-07-10 thomas if (*abspath == NULL)
288 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
289 795b88cb 2023-07-10 thomas }
290 795b88cb 2023-07-10 thomas
291 795b88cb 2023-07-10 thomas return NULL;
292 795b88cb 2023-07-10 thomas }
293 795b88cb 2023-07-10 thomas
294 795b88cb 2023-07-10 thomas static const struct got_error *
295 795b88cb 2023-07-10 thomas apply_unveil(const char *repo_path, int repo_read_only,
296 795b88cb 2023-07-10 thomas const char *worktree_path)
297 795b88cb 2023-07-10 thomas {
298 795b88cb 2023-07-10 thomas const struct got_error *err;
299 795b88cb 2023-07-10 thomas
300 795b88cb 2023-07-10 thomas #ifdef PROFILE
301 795b88cb 2023-07-10 thomas if (unveil("gmon.out", "rwc") != 0)
302 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", "gmon.out");
303 795b88cb 2023-07-10 thomas #endif
304 795b88cb 2023-07-10 thomas if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
305 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", repo_path);
306 795b88cb 2023-07-10 thomas
307 795b88cb 2023-07-10 thomas if (worktree_path && unveil(worktree_path, "rwc") != 0)
308 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", worktree_path);
309 795b88cb 2023-07-10 thomas
310 795b88cb 2023-07-10 thomas if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
311 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 795b88cb 2023-07-10 thomas
313 795b88cb 2023-07-10 thomas err = got_privsep_unveil_exec_helpers();
314 795b88cb 2023-07-10 thomas if (err != NULL)
315 795b88cb 2023-07-10 thomas return err;
316 795b88cb 2023-07-10 thomas
317 795b88cb 2023-07-10 thomas if (unveil(NULL, NULL) != 0)
318 795b88cb 2023-07-10 thomas return got_error_from_errno("unveil");
319 795b88cb 2023-07-10 thomas
320 795b88cb 2023-07-10 thomas return NULL;
321 795b88cb 2023-07-10 thomas }
322 795b88cb 2023-07-10 thomas
323 795b88cb 2023-07-10 thomas __dead static void
324 795b88cb 2023-07-10 thomas usage_import(void)
325 795b88cb 2023-07-10 thomas {
326 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
327 795b88cb 2023-07-10 thomas "[-r repository-path] directory\n", getprogname());
328 795b88cb 2023-07-10 thomas exit(1);
329 795b88cb 2023-07-10 thomas }
330 795b88cb 2023-07-10 thomas
331 795b88cb 2023-07-10 thomas static int
332 795b88cb 2023-07-10 thomas spawn_editor(const char *editor, const char *file)
333 795b88cb 2023-07-10 thomas {
334 795b88cb 2023-07-10 thomas pid_t pid;
335 795b88cb 2023-07-10 thomas sig_t sighup, sigint, sigquit;
336 795b88cb 2023-07-10 thomas int st = -1;
337 795b88cb 2023-07-10 thomas
338 795b88cb 2023-07-10 thomas sighup = signal(SIGHUP, SIG_IGN);
339 795b88cb 2023-07-10 thomas sigint = signal(SIGINT, SIG_IGN);
340 795b88cb 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_IGN);
341 795b88cb 2023-07-10 thomas
342 795b88cb 2023-07-10 thomas switch (pid = fork()) {
343 795b88cb 2023-07-10 thomas case -1:
344 795b88cb 2023-07-10 thomas goto doneediting;
345 795b88cb 2023-07-10 thomas case 0:
346 795b88cb 2023-07-10 thomas execl(editor, editor, file, (char *)NULL);
347 795b88cb 2023-07-10 thomas _exit(127);
348 795b88cb 2023-07-10 thomas }
349 795b88cb 2023-07-10 thomas
350 795b88cb 2023-07-10 thomas while (waitpid(pid, &st, 0) == -1)
351 795b88cb 2023-07-10 thomas if (errno != EINTR)
352 795b88cb 2023-07-10 thomas break;
353 795b88cb 2023-07-10 thomas
354 795b88cb 2023-07-10 thomas doneediting:
355 795b88cb 2023-07-10 thomas (void)signal(SIGHUP, sighup);
356 795b88cb 2023-07-10 thomas (void)signal(SIGINT, sigint);
357 795b88cb 2023-07-10 thomas (void)signal(SIGQUIT, sigquit);
358 795b88cb 2023-07-10 thomas
359 795b88cb 2023-07-10 thomas if (!WIFEXITED(st)) {
360 795b88cb 2023-07-10 thomas errno = EINTR;
361 795b88cb 2023-07-10 thomas return -1;
362 795b88cb 2023-07-10 thomas }
363 795b88cb 2023-07-10 thomas
364 795b88cb 2023-07-10 thomas return WEXITSTATUS(st);
365 795b88cb 2023-07-10 thomas }
366 795b88cb 2023-07-10 thomas
367 795b88cb 2023-07-10 thomas static const struct got_error *
368 795b88cb 2023-07-10 thomas read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
369 795b88cb 2023-07-10 thomas {
370 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
371 795b88cb 2023-07-10 thomas char *line = NULL;
372 795b88cb 2023-07-10 thomas size_t linesize = 0;
373 795b88cb 2023-07-10 thomas
374 795b88cb 2023-07-10 thomas *logmsg = NULL;
375 795b88cb 2023-07-10 thomas *len = 0;
376 795b88cb 2023-07-10 thomas
377 795b88cb 2023-07-10 thomas if (fseeko(fp, 0L, SEEK_SET) == -1)
378 795b88cb 2023-07-10 thomas return got_error_from_errno("fseeko");
379 795b88cb 2023-07-10 thomas
380 795b88cb 2023-07-10 thomas *logmsg = malloc(filesize + 1);
381 795b88cb 2023-07-10 thomas if (*logmsg == NULL)
382 795b88cb 2023-07-10 thomas return got_error_from_errno("malloc");
383 795b88cb 2023-07-10 thomas (*logmsg)[0] = '\0';
384 795b88cb 2023-07-10 thomas
385 795b88cb 2023-07-10 thomas while (getline(&line, &linesize, fp) != -1) {
386 795b88cb 2023-07-10 thomas if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
387 795b88cb 2023-07-10 thomas continue; /* remove comments and leading empty lines */
388 795b88cb 2023-07-10 thomas *len = strlcat(*logmsg, line, filesize + 1);
389 795b88cb 2023-07-10 thomas if (*len >= filesize + 1) {
390 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
391 795b88cb 2023-07-10 thomas goto done;
392 795b88cb 2023-07-10 thomas }
393 795b88cb 2023-07-10 thomas }
394 795b88cb 2023-07-10 thomas if (ferror(fp)) {
395 795b88cb 2023-07-10 thomas err = got_ferror(fp, GOT_ERR_IO);
396 795b88cb 2023-07-10 thomas goto done;
397 795b88cb 2023-07-10 thomas }
398 795b88cb 2023-07-10 thomas
399 795b88cb 2023-07-10 thomas while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
400 795b88cb 2023-07-10 thomas (*logmsg)[*len - 1] = '\0';
401 795b88cb 2023-07-10 thomas (*len)--;
402 795b88cb 2023-07-10 thomas }
403 795b88cb 2023-07-10 thomas done:
404 795b88cb 2023-07-10 thomas free(line);
405 795b88cb 2023-07-10 thomas if (err) {
406 795b88cb 2023-07-10 thomas free(*logmsg);
407 795b88cb 2023-07-10 thomas *logmsg = NULL;
408 795b88cb 2023-07-10 thomas *len = 0;
409 795b88cb 2023-07-10 thomas }
410 795b88cb 2023-07-10 thomas return err;
411 795b88cb 2023-07-10 thomas }
412 795b88cb 2023-07-10 thomas
413 795b88cb 2023-07-10 thomas static const struct got_error *
414 795b88cb 2023-07-10 thomas edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 795b88cb 2023-07-10 thomas const char *initial_content, size_t initial_content_len,
416 795b88cb 2023-07-10 thomas int require_modification)
417 795b88cb 2023-07-10 thomas {
418 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
419 795b88cb 2023-07-10 thomas struct stat st, st2;
420 795b88cb 2023-07-10 thomas FILE *fp = NULL;
421 795b88cb 2023-07-10 thomas size_t logmsg_len;
422 795b88cb 2023-07-10 thomas
423 795b88cb 2023-07-10 thomas *logmsg = NULL;
424 795b88cb 2023-07-10 thomas
425 795b88cb 2023-07-10 thomas if (stat(logmsg_path, &st) == -1)
426 795b88cb 2023-07-10 thomas return got_error_from_errno2("stat", logmsg_path);
427 795b88cb 2023-07-10 thomas
428 795b88cb 2023-07-10 thomas if (spawn_editor(editor, logmsg_path) == -1)
429 795b88cb 2023-07-10 thomas return got_error_from_errno("failed spawning editor");
430 795b88cb 2023-07-10 thomas
431 795b88cb 2023-07-10 thomas if (require_modification) {
432 795b88cb 2023-07-10 thomas struct timespec timeout;
433 795b88cb 2023-07-10 thomas
434 795b88cb 2023-07-10 thomas timeout.tv_sec = 0;
435 795b88cb 2023-07-10 thomas timeout.tv_nsec = 1;
436 795b88cb 2023-07-10 thomas nanosleep(&timeout, NULL);
437 795b88cb 2023-07-10 thomas }
438 795b88cb 2023-07-10 thomas
439 795b88cb 2023-07-10 thomas if (stat(logmsg_path, &st2) == -1)
440 5541355f 2023-07-17 thomas return got_error_from_errno2("stat", logmsg_path);
441 795b88cb 2023-07-10 thomas
442 795b88cb 2023-07-10 thomas if (require_modification && st.st_size == st2.st_size &&
443 795b88cb 2023-07-10 thomas timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
444 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
445 795b88cb 2023-07-10 thomas "no changes made to commit message, aborting");
446 795b88cb 2023-07-10 thomas
447 795b88cb 2023-07-10 thomas fp = fopen(logmsg_path, "re");
448 795b88cb 2023-07-10 thomas if (fp == NULL) {
449 795b88cb 2023-07-10 thomas err = got_error_from_errno("fopen");
450 795b88cb 2023-07-10 thomas goto done;
451 795b88cb 2023-07-10 thomas }
452 795b88cb 2023-07-10 thomas
453 795b88cb 2023-07-10 thomas /* strip comments and leading/trailing newlines */
454 795b88cb 2023-07-10 thomas err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
455 795b88cb 2023-07-10 thomas if (err)
456 795b88cb 2023-07-10 thomas goto done;
457 795b88cb 2023-07-10 thomas if (logmsg_len == 0) {
458 795b88cb 2023-07-10 thomas err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
459 795b88cb 2023-07-10 thomas "commit message cannot be empty, aborting");
460 795b88cb 2023-07-10 thomas goto done;
461 795b88cb 2023-07-10 thomas }
462 795b88cb 2023-07-10 thomas done:
463 795b88cb 2023-07-10 thomas if (fp && fclose(fp) == EOF && err == NULL)
464 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
465 795b88cb 2023-07-10 thomas if (err) {
466 795b88cb 2023-07-10 thomas free(*logmsg);
467 795b88cb 2023-07-10 thomas *logmsg = NULL;
468 795b88cb 2023-07-10 thomas }
469 795b88cb 2023-07-10 thomas return err;
470 795b88cb 2023-07-10 thomas }
471 795b88cb 2023-07-10 thomas
472 795b88cb 2023-07-10 thomas static const struct got_error *
473 795b88cb 2023-07-10 thomas collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 795b88cb 2023-07-10 thomas const char *path_dir, const char *branch_name)
475 795b88cb 2023-07-10 thomas {
476 795b88cb 2023-07-10 thomas char *initial_content = NULL;
477 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
478 795b88cb 2023-07-10 thomas int initial_content_len;
479 795b88cb 2023-07-10 thomas int fd = -1;
480 795b88cb 2023-07-10 thomas
481 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
482 795b88cb 2023-07-10 thomas "\n# %s to be imported to branch %s\n", path_dir,
483 795b88cb 2023-07-10 thomas branch_name);
484 795b88cb 2023-07-10 thomas if (initial_content_len == -1)
485 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
486 795b88cb 2023-07-10 thomas
487 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(logmsg_path, &fd,
488 795b88cb 2023-07-10 thomas GOT_TMPDIR_STR "/got-importmsg", "");
489 795b88cb 2023-07-10 thomas if (err)
490 795b88cb 2023-07-10 thomas goto done;
491 795b88cb 2023-07-10 thomas
492 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
493 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", *logmsg_path);
494 795b88cb 2023-07-10 thomas goto done;
495 795b88cb 2023-07-10 thomas }
496 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
497 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
498 795b88cb 2023-07-10 thomas goto done;
499 795b88cb 2023-07-10 thomas }
500 795b88cb 2023-07-10 thomas fd = -1;
501 795b88cb 2023-07-10 thomas
502 795b88cb 2023-07-10 thomas err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
503 795b88cb 2023-07-10 thomas initial_content_len, 1);
504 795b88cb 2023-07-10 thomas done:
505 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
506 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
507 795b88cb 2023-07-10 thomas free(initial_content);
508 795b88cb 2023-07-10 thomas if (err) {
509 795b88cb 2023-07-10 thomas free(*logmsg_path);
510 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
511 795b88cb 2023-07-10 thomas }
512 795b88cb 2023-07-10 thomas return err;
513 795b88cb 2023-07-10 thomas }
514 795b88cb 2023-07-10 thomas
515 795b88cb 2023-07-10 thomas static const struct got_error *
516 795b88cb 2023-07-10 thomas import_progress(void *arg, const char *path)
517 795b88cb 2023-07-10 thomas {
518 795b88cb 2023-07-10 thomas printf("A %s\n", path);
519 795b88cb 2023-07-10 thomas return NULL;
520 795b88cb 2023-07-10 thomas }
521 795b88cb 2023-07-10 thomas
522 795b88cb 2023-07-10 thomas static const struct got_error *
523 795b88cb 2023-07-10 thomas valid_author(const char *author)
524 795b88cb 2023-07-10 thomas {
525 795b88cb 2023-07-10 thomas const char *email = author;
526 795b88cb 2023-07-10 thomas
527 795b88cb 2023-07-10 thomas /*
528 795b88cb 2023-07-10 thomas * Git' expects the author (or committer) to be in the form
529 795b88cb 2023-07-10 thomas * "name <email>", which are mostly free form (see the
530 795b88cb 2023-07-10 thomas * "committer" description in git-fast-import(1)). We're only
531 795b88cb 2023-07-10 thomas * doing this to avoid git's object parser breaking on commits
532 795b88cb 2023-07-10 thomas * we create.
533 795b88cb 2023-07-10 thomas */
534 795b88cb 2023-07-10 thomas
535 795b88cb 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
536 795b88cb 2023-07-10 thomas author++;
537 795b88cb 2023-07-10 thomas if (author != email && *author == '<' && *(author - 1) != ' ')
538 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
539 795b88cb 2023-07-10 thomas "between author name and email required", email);
540 795b88cb 2023-07-10 thomas if (*author++ != '<')
541 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
542 795b88cb 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
543 795b88cb 2023-07-10 thomas author++;
544 795b88cb 2023-07-10 thomas if (strcmp(author, ">") != 0)
545 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
546 795b88cb 2023-07-10 thomas return NULL;
547 795b88cb 2023-07-10 thomas }
548 795b88cb 2023-07-10 thomas
549 795b88cb 2023-07-10 thomas static const struct got_error *
550 795b88cb 2023-07-10 thomas get_author(char **author, struct got_repository *repo,
551 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
552 795b88cb 2023-07-10 thomas {
553 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
554 795b88cb 2023-07-10 thomas const char *got_author = NULL, *name, *email;
555 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
556 795b88cb 2023-07-10 thomas
557 795b88cb 2023-07-10 thomas *author = NULL;
558 795b88cb 2023-07-10 thomas
559 795b88cb 2023-07-10 thomas if (worktree)
560 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
561 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
562 795b88cb 2023-07-10 thomas
563 795b88cb 2023-07-10 thomas /*
564 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
565 795b88cb 2023-07-10 thomas * significant to least significant:
566 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
567 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
568 795b88cb 2023-07-10 thomas * 3) repository's git config file
569 795b88cb 2023-07-10 thomas * 4) environment variables
570 795b88cb 2023-07-10 thomas * 5) global git config files (in user's home directory or /etc)
571 795b88cb 2023-07-10 thomas */
572 795b88cb 2023-07-10 thomas
573 795b88cb 2023-07-10 thomas if (worktree_conf)
574 795b88cb 2023-07-10 thomas got_author = got_gotconfig_get_author(worktree_conf);
575 795b88cb 2023-07-10 thomas if (got_author == NULL)
576 795b88cb 2023-07-10 thomas got_author = got_gotconfig_get_author(repo_conf);
577 795b88cb 2023-07-10 thomas if (got_author == NULL) {
578 795b88cb 2023-07-10 thomas name = got_repo_get_gitconfig_author_name(repo);
579 795b88cb 2023-07-10 thomas email = got_repo_get_gitconfig_author_email(repo);
580 795b88cb 2023-07-10 thomas if (name && email) {
581 795b88cb 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email) == -1)
582 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
583 795b88cb 2023-07-10 thomas return NULL;
584 795b88cb 2023-07-10 thomas }
585 795b88cb 2023-07-10 thomas
586 795b88cb 2023-07-10 thomas got_author = getenv("GOT_AUTHOR");
587 795b88cb 2023-07-10 thomas if (got_author == NULL) {
588 795b88cb 2023-07-10 thomas name = got_repo_get_global_gitconfig_author_name(repo);
589 795b88cb 2023-07-10 thomas email = got_repo_get_global_gitconfig_author_email(
590 795b88cb 2023-07-10 thomas repo);
591 795b88cb 2023-07-10 thomas if (name && email) {
592 795b88cb 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email)
593 795b88cb 2023-07-10 thomas == -1)
594 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
595 795b88cb 2023-07-10 thomas return NULL;
596 795b88cb 2023-07-10 thomas }
597 795b88cb 2023-07-10 thomas /* TODO: Look up user in password database? */
598 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
599 795b88cb 2023-07-10 thomas }
600 795b88cb 2023-07-10 thomas }
601 795b88cb 2023-07-10 thomas
602 795b88cb 2023-07-10 thomas *author = strdup(got_author);
603 795b88cb 2023-07-10 thomas if (*author == NULL)
604 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
605 795b88cb 2023-07-10 thomas
606 795b88cb 2023-07-10 thomas err = valid_author(*author);
607 795b88cb 2023-07-10 thomas if (err) {
608 795b88cb 2023-07-10 thomas free(*author);
609 795b88cb 2023-07-10 thomas *author = NULL;
610 795b88cb 2023-07-10 thomas }
611 795b88cb 2023-07-10 thomas return err;
612 795b88cb 2023-07-10 thomas }
613 795b88cb 2023-07-10 thomas
614 795b88cb 2023-07-10 thomas static const struct got_error *
615 795b88cb 2023-07-10 thomas get_allowed_signers(char **allowed_signers, struct got_repository *repo,
616 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
617 795b88cb 2023-07-10 thomas {
618 795b88cb 2023-07-10 thomas const char *got_allowed_signers = NULL;
619 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
620 795b88cb 2023-07-10 thomas
621 795b88cb 2023-07-10 thomas *allowed_signers = NULL;
622 795b88cb 2023-07-10 thomas
623 795b88cb 2023-07-10 thomas if (worktree)
624 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
625 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
626 795b88cb 2023-07-10 thomas
627 795b88cb 2023-07-10 thomas /*
628 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
629 795b88cb 2023-07-10 thomas * significant to least significant:
630 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
631 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
632 795b88cb 2023-07-10 thomas */
633 795b88cb 2023-07-10 thomas
634 795b88cb 2023-07-10 thomas if (worktree_conf)
635 795b88cb 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
636 795b88cb 2023-07-10 thomas worktree_conf);
637 795b88cb 2023-07-10 thomas if (got_allowed_signers == NULL)
638 795b88cb 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
639 795b88cb 2023-07-10 thomas repo_conf);
640 795b88cb 2023-07-10 thomas
641 795b88cb 2023-07-10 thomas if (got_allowed_signers) {
642 795b88cb 2023-07-10 thomas *allowed_signers = strdup(got_allowed_signers);
643 795b88cb 2023-07-10 thomas if (*allowed_signers == NULL)
644 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
645 795b88cb 2023-07-10 thomas }
646 795b88cb 2023-07-10 thomas return NULL;
647 795b88cb 2023-07-10 thomas }
648 795b88cb 2023-07-10 thomas
649 795b88cb 2023-07-10 thomas static const struct got_error *
650 795b88cb 2023-07-10 thomas get_revoked_signers(char **revoked_signers, struct got_repository *repo,
651 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
652 795b88cb 2023-07-10 thomas {
653 795b88cb 2023-07-10 thomas const char *got_revoked_signers = NULL;
654 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
655 795b88cb 2023-07-10 thomas
656 795b88cb 2023-07-10 thomas *revoked_signers = NULL;
657 795b88cb 2023-07-10 thomas
658 795b88cb 2023-07-10 thomas if (worktree)
659 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
660 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
661 795b88cb 2023-07-10 thomas
662 795b88cb 2023-07-10 thomas /*
663 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
664 795b88cb 2023-07-10 thomas * significant to least significant:
665 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
666 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
667 795b88cb 2023-07-10 thomas */
668 795b88cb 2023-07-10 thomas
669 795b88cb 2023-07-10 thomas if (worktree_conf)
670 795b88cb 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
671 795b88cb 2023-07-10 thomas worktree_conf);
672 795b88cb 2023-07-10 thomas if (got_revoked_signers == NULL)
673 795b88cb 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
674 795b88cb 2023-07-10 thomas repo_conf);
675 795b88cb 2023-07-10 thomas
676 795b88cb 2023-07-10 thomas if (got_revoked_signers) {
677 795b88cb 2023-07-10 thomas *revoked_signers = strdup(got_revoked_signers);
678 795b88cb 2023-07-10 thomas if (*revoked_signers == NULL)
679 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
680 795b88cb 2023-07-10 thomas }
681 795b88cb 2023-07-10 thomas return NULL;
682 795b88cb 2023-07-10 thomas }
683 795b88cb 2023-07-10 thomas
684 795b88cb 2023-07-10 thomas static const char *
685 795b88cb 2023-07-10 thomas get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
686 795b88cb 2023-07-10 thomas {
687 795b88cb 2023-07-10 thomas const char *got_signer_id = NULL;
688 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
689 795b88cb 2023-07-10 thomas
690 795b88cb 2023-07-10 thomas if (worktree)
691 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
692 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
693 795b88cb 2023-07-10 thomas
694 795b88cb 2023-07-10 thomas /*
695 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
696 795b88cb 2023-07-10 thomas * significant to least significant:
697 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
698 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
699 795b88cb 2023-07-10 thomas */
700 795b88cb 2023-07-10 thomas
701 795b88cb 2023-07-10 thomas if (worktree_conf)
702 795b88cb 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
703 795b88cb 2023-07-10 thomas if (got_signer_id == NULL)
704 795b88cb 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(repo_conf);
705 795b88cb 2023-07-10 thomas
706 795b88cb 2023-07-10 thomas return got_signer_id;
707 795b88cb 2023-07-10 thomas }
708 795b88cb 2023-07-10 thomas
709 795b88cb 2023-07-10 thomas static const struct got_error *
710 795b88cb 2023-07-10 thomas get_gitconfig_path(char **gitconfig_path)
711 795b88cb 2023-07-10 thomas {
712 795b88cb 2023-07-10 thomas const char *homedir = getenv("HOME");
713 795b88cb 2023-07-10 thomas
714 795b88cb 2023-07-10 thomas *gitconfig_path = NULL;
715 795b88cb 2023-07-10 thomas if (homedir) {
716 795b88cb 2023-07-10 thomas if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
717 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
718 795b88cb 2023-07-10 thomas
719 795b88cb 2023-07-10 thomas }
720 795b88cb 2023-07-10 thomas return NULL;
721 795b88cb 2023-07-10 thomas }
722 795b88cb 2023-07-10 thomas
723 795b88cb 2023-07-10 thomas static const struct got_error *
724 795b88cb 2023-07-10 thomas cmd_import(int argc, char *argv[])
725 795b88cb 2023-07-10 thomas {
726 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
727 795b88cb 2023-07-10 thomas char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
728 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
729 795b88cb 2023-07-10 thomas const char *branch_name = NULL;
730 795b88cb 2023-07-10 thomas char *id_str = NULL, *logmsg_path = NULL;
731 795b88cb 2023-07-10 thomas char refname[PATH_MAX] = "refs/heads/";
732 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
733 795b88cb 2023-07-10 thomas struct got_reference *branch_ref = NULL, *head_ref = NULL;
734 795b88cb 2023-07-10 thomas struct got_object_id *new_commit_id = NULL;
735 795b88cb 2023-07-10 thomas int ch, n = 0;
736 795b88cb 2023-07-10 thomas struct got_pathlist_head ignores;
737 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
738 795b88cb 2023-07-10 thomas int preserve_logmsg = 0;
739 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
740 795b88cb 2023-07-10 thomas
741 795b88cb 2023-07-10 thomas TAILQ_INIT(&ignores);
742 795b88cb 2023-07-10 thomas
743 795b88cb 2023-07-10 thomas #ifndef PROFILE
744 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
745 795b88cb 2023-07-10 thomas "unveil",
746 795b88cb 2023-07-10 thomas NULL) == -1)
747 795b88cb 2023-07-10 thomas err(1, "pledge");
748 795b88cb 2023-07-10 thomas #endif
749 795b88cb 2023-07-10 thomas
750 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
751 795b88cb 2023-07-10 thomas switch (ch) {
752 795b88cb 2023-07-10 thomas case 'b':
753 795b88cb 2023-07-10 thomas branch_name = optarg;
754 795b88cb 2023-07-10 thomas break;
755 795b88cb 2023-07-10 thomas case 'I':
756 795b88cb 2023-07-10 thomas if (optarg[0] == '\0')
757 795b88cb 2023-07-10 thomas break;
758 795b88cb 2023-07-10 thomas error = got_pathlist_insert(&pe, &ignores, optarg,
759 795b88cb 2023-07-10 thomas NULL);
760 795b88cb 2023-07-10 thomas if (error)
761 795b88cb 2023-07-10 thomas goto done;
762 795b88cb 2023-07-10 thomas break;
763 795b88cb 2023-07-10 thomas case 'm':
764 795b88cb 2023-07-10 thomas logmsg = strdup(optarg);
765 795b88cb 2023-07-10 thomas if (logmsg == NULL) {
766 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
767 795b88cb 2023-07-10 thomas goto done;
768 795b88cb 2023-07-10 thomas }
769 795b88cb 2023-07-10 thomas break;
770 795b88cb 2023-07-10 thomas case 'r':
771 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
772 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
773 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
774 795b88cb 2023-07-10 thomas optarg);
775 795b88cb 2023-07-10 thomas goto done;
776 795b88cb 2023-07-10 thomas }
777 795b88cb 2023-07-10 thomas break;
778 795b88cb 2023-07-10 thomas default:
779 795b88cb 2023-07-10 thomas usage_import();
780 795b88cb 2023-07-10 thomas /* NOTREACHED */
781 795b88cb 2023-07-10 thomas }
782 795b88cb 2023-07-10 thomas }
783 795b88cb 2023-07-10 thomas
784 795b88cb 2023-07-10 thomas argc -= optind;
785 795b88cb 2023-07-10 thomas argv += optind;
786 795b88cb 2023-07-10 thomas
787 795b88cb 2023-07-10 thomas if (argc != 1)
788 795b88cb 2023-07-10 thomas usage_import();
789 795b88cb 2023-07-10 thomas
790 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
791 795b88cb 2023-07-10 thomas repo_path = getcwd(NULL, 0);
792 795b88cb 2023-07-10 thomas if (repo_path == NULL)
793 795b88cb 2023-07-10 thomas return got_error_from_errno("getcwd");
794 795b88cb 2023-07-10 thomas }
795 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
796 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
797 795b88cb 2023-07-10 thomas if (error)
798 795b88cb 2023-07-10 thomas goto done;
799 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
800 795b88cb 2023-07-10 thomas if (error != NULL)
801 795b88cb 2023-07-10 thomas goto done;
802 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
803 795b88cb 2023-07-10 thomas if (error)
804 795b88cb 2023-07-10 thomas goto done;
805 795b88cb 2023-07-10 thomas
806 795b88cb 2023-07-10 thomas error = get_author(&author, repo, NULL);
807 795b88cb 2023-07-10 thomas if (error)
808 795b88cb 2023-07-10 thomas return error;
809 795b88cb 2023-07-10 thomas
810 795b88cb 2023-07-10 thomas /*
811 795b88cb 2023-07-10 thomas * Don't let the user create a branch name with a leading '-'.
812 795b88cb 2023-07-10 thomas * While technically a valid reference name, this case is usually
813 795b88cb 2023-07-10 thomas * an unintended typo.
814 795b88cb 2023-07-10 thomas */
815 795b88cb 2023-07-10 thomas if (branch_name && branch_name[0] == '-')
816 795b88cb 2023-07-10 thomas return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
817 795b88cb 2023-07-10 thomas
818 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
819 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_REF)
820 795b88cb 2023-07-10 thomas goto done;
821 795b88cb 2023-07-10 thomas
822 795b88cb 2023-07-10 thomas if (branch_name)
823 795b88cb 2023-07-10 thomas n = strlcat(refname, branch_name, sizeof(refname));
824 795b88cb 2023-07-10 thomas else if (head_ref && got_ref_is_symbolic(head_ref))
825 795b88cb 2023-07-10 thomas n = strlcpy(refname, got_ref_get_symref_target(head_ref),
826 795b88cb 2023-07-10 thomas sizeof(refname));
827 795b88cb 2023-07-10 thomas else
828 795b88cb 2023-07-10 thomas n = strlcat(refname, "main", sizeof(refname));
829 795b88cb 2023-07-10 thomas if (n >= sizeof(refname)) {
830 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_NO_SPACE);
831 795b88cb 2023-07-10 thomas goto done;
832 795b88cb 2023-07-10 thomas }
833 795b88cb 2023-07-10 thomas
834 795b88cb 2023-07-10 thomas error = got_ref_open(&branch_ref, repo, refname, 0);
835 795b88cb 2023-07-10 thomas if (error) {
836 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
837 795b88cb 2023-07-10 thomas goto done;
838 795b88cb 2023-07-10 thomas } else {
839 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
840 795b88cb 2023-07-10 thomas "import target branch already exists");
841 795b88cb 2023-07-10 thomas goto done;
842 795b88cb 2023-07-10 thomas }
843 795b88cb 2023-07-10 thomas
844 795b88cb 2023-07-10 thomas path_dir = realpath(argv[0], NULL);
845 795b88cb 2023-07-10 thomas if (path_dir == NULL) {
846 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
847 795b88cb 2023-07-10 thomas goto done;
848 795b88cb 2023-07-10 thomas }
849 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(path_dir);
850 795b88cb 2023-07-10 thomas
851 795b88cb 2023-07-10 thomas /*
852 795b88cb 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
853 795b88cb 2023-07-10 thomas * to apply unveil after the log message has been written.
854 795b88cb 2023-07-10 thomas */
855 795b88cb 2023-07-10 thomas if (logmsg == NULL || *logmsg == '\0') {
856 795b88cb 2023-07-10 thomas error = get_editor(&editor);
857 795b88cb 2023-07-10 thomas if (error)
858 795b88cb 2023-07-10 thomas goto done;
859 795b88cb 2023-07-10 thomas free(logmsg);
860 795b88cb 2023-07-10 thomas error = collect_import_msg(&logmsg, &logmsg_path, editor,
861 795b88cb 2023-07-10 thomas path_dir, refname);
862 795b88cb 2023-07-10 thomas if (error) {
863 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
864 795b88cb 2023-07-10 thomas logmsg_path != NULL)
865 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
866 795b88cb 2023-07-10 thomas goto done;
867 795b88cb 2023-07-10 thomas }
868 795b88cb 2023-07-10 thomas }
869 795b88cb 2023-07-10 thomas
870 795b88cb 2023-07-10 thomas if (unveil(path_dir, "r") != 0) {
871 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unveil", path_dir);
872 795b88cb 2023-07-10 thomas if (logmsg_path)
873 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
874 795b88cb 2023-07-10 thomas goto done;
875 795b88cb 2023-07-10 thomas }
876 795b88cb 2023-07-10 thomas
877 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
878 795b88cb 2023-07-10 thomas if (error) {
879 795b88cb 2023-07-10 thomas if (logmsg_path)
880 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
881 795b88cb 2023-07-10 thomas goto done;
882 795b88cb 2023-07-10 thomas }
883 795b88cb 2023-07-10 thomas
884 795b88cb 2023-07-10 thomas error = got_repo_import(&new_commit_id, path_dir, logmsg,
885 795b88cb 2023-07-10 thomas author, &ignores, repo, import_progress, NULL);
886 795b88cb 2023-07-10 thomas if (error) {
887 795b88cb 2023-07-10 thomas if (logmsg_path)
888 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
889 795b88cb 2023-07-10 thomas goto done;
890 795b88cb 2023-07-10 thomas }
891 795b88cb 2023-07-10 thomas
892 795b88cb 2023-07-10 thomas error = got_ref_alloc(&branch_ref, refname, new_commit_id);
893 795b88cb 2023-07-10 thomas if (error) {
894 795b88cb 2023-07-10 thomas if (logmsg_path)
895 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
896 795b88cb 2023-07-10 thomas goto done;
897 795b88cb 2023-07-10 thomas }
898 795b88cb 2023-07-10 thomas
899 795b88cb 2023-07-10 thomas error = got_ref_write(branch_ref, repo);
900 795b88cb 2023-07-10 thomas if (error) {
901 795b88cb 2023-07-10 thomas if (logmsg_path)
902 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
903 795b88cb 2023-07-10 thomas goto done;
904 795b88cb 2023-07-10 thomas }
905 795b88cb 2023-07-10 thomas
906 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, new_commit_id);
907 795b88cb 2023-07-10 thomas if (error) {
908 795b88cb 2023-07-10 thomas if (logmsg_path)
909 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
910 795b88cb 2023-07-10 thomas goto done;
911 795b88cb 2023-07-10 thomas }
912 795b88cb 2023-07-10 thomas
913 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
914 795b88cb 2023-07-10 thomas if (error) {
915 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF) {
916 795b88cb 2023-07-10 thomas if (logmsg_path)
917 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
918 795b88cb 2023-07-10 thomas goto done;
919 795b88cb 2023-07-10 thomas }
920 795b88cb 2023-07-10 thomas
921 795b88cb 2023-07-10 thomas error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
922 795b88cb 2023-07-10 thomas branch_ref);
923 795b88cb 2023-07-10 thomas if (error) {
924 795b88cb 2023-07-10 thomas if (logmsg_path)
925 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
926 795b88cb 2023-07-10 thomas goto done;
927 795b88cb 2023-07-10 thomas }
928 795b88cb 2023-07-10 thomas
929 795b88cb 2023-07-10 thomas error = got_ref_write(head_ref, repo);
930 795b88cb 2023-07-10 thomas if (error) {
931 795b88cb 2023-07-10 thomas if (logmsg_path)
932 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
933 795b88cb 2023-07-10 thomas goto done;
934 795b88cb 2023-07-10 thomas }
935 795b88cb 2023-07-10 thomas }
936 795b88cb 2023-07-10 thomas
937 795b88cb 2023-07-10 thomas printf("Created branch %s with commit %s\n",
938 795b88cb 2023-07-10 thomas got_ref_get_name(branch_ref), id_str);
939 795b88cb 2023-07-10 thomas done:
940 795b88cb 2023-07-10 thomas if (pack_fds) {
941 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
942 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
943 795b88cb 2023-07-10 thomas if (error == NULL)
944 795b88cb 2023-07-10 thomas error = pack_err;
945 795b88cb 2023-07-10 thomas }
946 795b88cb 2023-07-10 thomas if (repo) {
947 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
948 795b88cb 2023-07-10 thomas if (error == NULL)
949 795b88cb 2023-07-10 thomas error = close_err;
950 795b88cb 2023-07-10 thomas }
951 795b88cb 2023-07-10 thomas if (preserve_logmsg) {
952 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
953 795b88cb 2023-07-10 thomas getprogname(), logmsg_path);
954 795b88cb 2023-07-10 thomas } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
955 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", logmsg_path);
956 795b88cb 2023-07-10 thomas free(logmsg);
957 795b88cb 2023-07-10 thomas free(logmsg_path);
958 795b88cb 2023-07-10 thomas free(repo_path);
959 795b88cb 2023-07-10 thomas free(editor);
960 795b88cb 2023-07-10 thomas free(new_commit_id);
961 795b88cb 2023-07-10 thomas free(id_str);
962 795b88cb 2023-07-10 thomas free(author);
963 795b88cb 2023-07-10 thomas free(gitconfig_path);
964 795b88cb 2023-07-10 thomas if (branch_ref)
965 795b88cb 2023-07-10 thomas got_ref_close(branch_ref);
966 795b88cb 2023-07-10 thomas if (head_ref)
967 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
968 795b88cb 2023-07-10 thomas return error;
969 795b88cb 2023-07-10 thomas }
970 795b88cb 2023-07-10 thomas
971 795b88cb 2023-07-10 thomas __dead static void
972 795b88cb 2023-07-10 thomas usage_clone(void)
973 795b88cb 2023-07-10 thomas {
974 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
975 795b88cb 2023-07-10 thomas "repository-URL [directory]\n", getprogname());
976 795b88cb 2023-07-10 thomas exit(1);
977 795b88cb 2023-07-10 thomas }
978 795b88cb 2023-07-10 thomas
979 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg {
980 795b88cb 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
981 795b88cb 2023-07-10 thomas int last_p_indexed;
982 795b88cb 2023-07-10 thomas int last_p_resolved;
983 795b88cb 2023-07-10 thomas int verbosity;
984 795b88cb 2023-07-10 thomas
985 795b88cb 2023-07-10 thomas struct got_repository *repo;
986 795b88cb 2023-07-10 thomas
987 795b88cb 2023-07-10 thomas int create_configs;
988 795b88cb 2023-07-10 thomas int configs_created;
989 795b88cb 2023-07-10 thomas struct {
990 795b88cb 2023-07-10 thomas struct got_pathlist_head *symrefs;
991 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches;
992 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs;
993 795b88cb 2023-07-10 thomas const char *proto;
994 795b88cb 2023-07-10 thomas const char *host;
995 795b88cb 2023-07-10 thomas const char *port;
996 795b88cb 2023-07-10 thomas const char *remote_repo_path;
997 795b88cb 2023-07-10 thomas const char *git_url;
998 795b88cb 2023-07-10 thomas int fetch_all_branches;
999 795b88cb 2023-07-10 thomas int mirror_references;
1000 795b88cb 2023-07-10 thomas } config_info;
1001 795b88cb 2023-07-10 thomas };
1002 795b88cb 2023-07-10 thomas
1003 795b88cb 2023-07-10 thomas /* XXX forward declaration */
1004 795b88cb 2023-07-10 thomas static const struct got_error *
1005 795b88cb 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1006 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1007 795b88cb 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1008 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1009 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1010 795b88cb 2023-07-10 thomas
1011 795b88cb 2023-07-10 thomas static const struct got_error *
1012 795b88cb 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
1013 795b88cb 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1014 795b88cb 2023-07-10 thomas {
1015 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1016 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
1017 795b88cb 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
1018 795b88cb 2023-07-10 thomas int p_indexed, p_resolved;
1019 795b88cb 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
1020 795b88cb 2023-07-10 thomas
1021 795b88cb 2023-07-10 thomas /*
1022 795b88cb 2023-07-10 thomas * In order to allow a failed clone to be resumed with 'got fetch'
1023 795b88cb 2023-07-10 thomas * we try to create configuration files as soon as possible.
1024 795b88cb 2023-07-10 thomas * Once the server has sent information about its default branch
1025 795b88cb 2023-07-10 thomas * we have all required information.
1026 795b88cb 2023-07-10 thomas */
1027 795b88cb 2023-07-10 thomas if (a->create_configs && !a->configs_created &&
1028 795b88cb 2023-07-10 thomas !TAILQ_EMPTY(a->config_info.symrefs)) {
1029 795b88cb 2023-07-10 thomas err = create_config_files(a->config_info.proto,
1030 795b88cb 2023-07-10 thomas a->config_info.host, a->config_info.port,
1031 795b88cb 2023-07-10 thomas a->config_info.remote_repo_path,
1032 795b88cb 2023-07-10 thomas a->config_info.git_url,
1033 795b88cb 2023-07-10 thomas a->config_info.fetch_all_branches,
1034 795b88cb 2023-07-10 thomas a->config_info.mirror_references,
1035 795b88cb 2023-07-10 thomas a->config_info.symrefs,
1036 795b88cb 2023-07-10 thomas a->config_info.wanted_branches,
1037 795b88cb 2023-07-10 thomas a->config_info.wanted_refs, a->repo);
1038 795b88cb 2023-07-10 thomas if (err)
1039 795b88cb 2023-07-10 thomas return err;
1040 795b88cb 2023-07-10 thomas a->configs_created = 1;
1041 795b88cb 2023-07-10 thomas }
1042 795b88cb 2023-07-10 thomas
1043 795b88cb 2023-07-10 thomas if (a->verbosity < 0)
1044 795b88cb 2023-07-10 thomas return NULL;
1045 795b88cb 2023-07-10 thomas
1046 795b88cb 2023-07-10 thomas if (message && message[0] != '\0') {
1047 795b88cb 2023-07-10 thomas printf("\rserver: %s", message);
1048 795b88cb 2023-07-10 thomas fflush(stdout);
1049 795b88cb 2023-07-10 thomas return NULL;
1050 795b88cb 2023-07-10 thomas }
1051 795b88cb 2023-07-10 thomas
1052 795b88cb 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
1053 795b88cb 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1054 795b88cb 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
1055 795b88cb 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
1056 795b88cb 2023-07-10 thomas print_size = 1;
1057 795b88cb 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
1058 795b88cb 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1059 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
1060 795b88cb 2023-07-10 thomas }
1061 795b88cb 2023-07-10 thomas if (nobj_indexed > 0) {
1062 795b88cb 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
1063 795b88cb 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
1064 795b88cb 2023-07-10 thomas a->last_p_indexed = p_indexed;
1065 795b88cb 2023-07-10 thomas print_indexed = 1;
1066 795b88cb 2023-07-10 thomas print_size = 1;
1067 795b88cb 2023-07-10 thomas }
1068 795b88cb 2023-07-10 thomas }
1069 795b88cb 2023-07-10 thomas if (nobj_resolved > 0) {
1070 795b88cb 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
1071 795b88cb 2023-07-10 thomas (nobj_total - nobj_loose);
1072 795b88cb 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
1073 795b88cb 2023-07-10 thomas a->last_p_resolved = p_resolved;
1074 795b88cb 2023-07-10 thomas print_resolved = 1;
1075 795b88cb 2023-07-10 thomas print_indexed = 1;
1076 795b88cb 2023-07-10 thomas print_size = 1;
1077 795b88cb 2023-07-10 thomas }
1078 795b88cb 2023-07-10 thomas }
1079 795b88cb 2023-07-10 thomas
1080 795b88cb 2023-07-10 thomas }
1081 795b88cb 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1082 795b88cb 2023-07-10 thomas printf("\r");
1083 795b88cb 2023-07-10 thomas if (print_size)
1084 795b88cb 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1085 795b88cb 2023-07-10 thomas if (print_indexed)
1086 795b88cb 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
1087 795b88cb 2023-07-10 thomas if (print_resolved)
1088 795b88cb 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
1089 795b88cb 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1090 795b88cb 2023-07-10 thomas fflush(stdout);
1091 795b88cb 2023-07-10 thomas
1092 795b88cb 2023-07-10 thomas return NULL;
1093 795b88cb 2023-07-10 thomas }
1094 795b88cb 2023-07-10 thomas
1095 795b88cb 2023-07-10 thomas static const struct got_error *
1096 795b88cb 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
1097 795b88cb 2023-07-10 thomas int verbosity, struct got_repository *repo)
1098 795b88cb 2023-07-10 thomas {
1099 795b88cb 2023-07-10 thomas const struct got_error *err;
1100 795b88cb 2023-07-10 thomas struct got_reference *head_symref;
1101 795b88cb 2023-07-10 thomas
1102 795b88cb 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1103 795b88cb 2023-07-10 thomas if (err)
1104 795b88cb 2023-07-10 thomas return err;
1105 795b88cb 2023-07-10 thomas
1106 795b88cb 2023-07-10 thomas err = got_ref_write(head_symref, repo);
1107 795b88cb 2023-07-10 thomas if (err == NULL && verbosity > 0) {
1108 795b88cb 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
1109 795b88cb 2023-07-10 thomas got_ref_get_name(target_ref));
1110 795b88cb 2023-07-10 thomas }
1111 795b88cb 2023-07-10 thomas got_ref_close(head_symref);
1112 795b88cb 2023-07-10 thomas return err;
1113 795b88cb 2023-07-10 thomas }
1114 795b88cb 2023-07-10 thomas
1115 795b88cb 2023-07-10 thomas static const struct got_error *
1116 795b88cb 2023-07-10 thomas list_remote_refs(struct got_pathlist_head *symrefs,
1117 795b88cb 2023-07-10 thomas struct got_pathlist_head *refs)
1118 795b88cb 2023-07-10 thomas {
1119 795b88cb 2023-07-10 thomas const struct got_error *err;
1120 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1121 795b88cb 2023-07-10 thomas
1122 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1123 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1124 795b88cb 2023-07-10 thomas const char *targetref = pe->data;
1125 795b88cb 2023-07-10 thomas
1126 795b88cb 2023-07-10 thomas printf("%s: %s\n", refname, targetref);
1127 795b88cb 2023-07-10 thomas }
1128 795b88cb 2023-07-10 thomas
1129 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, refs, entry) {
1130 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1131 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
1132 795b88cb 2023-07-10 thomas char *id_str;
1133 795b88cb 2023-07-10 thomas
1134 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1135 795b88cb 2023-07-10 thomas if (err)
1136 795b88cb 2023-07-10 thomas return err;
1137 795b88cb 2023-07-10 thomas printf("%s: %s\n", refname, id_str);
1138 795b88cb 2023-07-10 thomas free(id_str);
1139 795b88cb 2023-07-10 thomas }
1140 795b88cb 2023-07-10 thomas
1141 795b88cb 2023-07-10 thomas return NULL;
1142 795b88cb 2023-07-10 thomas }
1143 795b88cb 2023-07-10 thomas
1144 795b88cb 2023-07-10 thomas static const struct got_error *
1145 795b88cb 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
1146 795b88cb 2023-07-10 thomas int verbosity, struct got_repository *repo)
1147 795b88cb 2023-07-10 thomas {
1148 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1149 795b88cb 2023-07-10 thomas struct got_reference *ref;
1150 795b88cb 2023-07-10 thomas char *id_str;
1151 795b88cb 2023-07-10 thomas
1152 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1153 795b88cb 2023-07-10 thomas if (err)
1154 795b88cb 2023-07-10 thomas return err;
1155 795b88cb 2023-07-10 thomas
1156 795b88cb 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
1157 795b88cb 2023-07-10 thomas if (err)
1158 795b88cb 2023-07-10 thomas goto done;
1159 795b88cb 2023-07-10 thomas
1160 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1161 795b88cb 2023-07-10 thomas got_ref_close(ref);
1162 795b88cb 2023-07-10 thomas
1163 795b88cb 2023-07-10 thomas if (err == NULL && verbosity >= 0)
1164 795b88cb 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
1165 795b88cb 2023-07-10 thomas done:
1166 795b88cb 2023-07-10 thomas free(id_str);
1167 795b88cb 2023-07-10 thomas return err;
1168 795b88cb 2023-07-10 thomas }
1169 795b88cb 2023-07-10 thomas
1170 795b88cb 2023-07-10 thomas static int
1171 795b88cb 2023-07-10 thomas match_wanted_ref(const char *refname, const char *wanted_ref)
1172 795b88cb 2023-07-10 thomas {
1173 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) != 0)
1174 795b88cb 2023-07-10 thomas return 0;
1175 795b88cb 2023-07-10 thomas refname += 5;
1176 795b88cb 2023-07-10 thomas
1177 795b88cb 2023-07-10 thomas /*
1178 795b88cb 2023-07-10 thomas * Prevent fetching of references that won't make any
1179 795b88cb 2023-07-10 thomas * sense outside of the remote repository's context.
1180 795b88cb 2023-07-10 thomas */
1181 795b88cb 2023-07-10 thomas if (strncmp(refname, "got/", 4) == 0)
1182 795b88cb 2023-07-10 thomas return 0;
1183 795b88cb 2023-07-10 thomas if (strncmp(refname, "remotes/", 8) == 0)
1184 795b88cb 2023-07-10 thomas return 0;
1185 795b88cb 2023-07-10 thomas
1186 795b88cb 2023-07-10 thomas if (strncmp(wanted_ref, "refs/", 5) == 0)
1187 795b88cb 2023-07-10 thomas wanted_ref += 5;
1188 795b88cb 2023-07-10 thomas
1189 795b88cb 2023-07-10 thomas /* Allow prefix match. */
1190 795b88cb 2023-07-10 thomas if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1191 795b88cb 2023-07-10 thomas return 1;
1192 795b88cb 2023-07-10 thomas
1193 795b88cb 2023-07-10 thomas /* Allow exact match. */
1194 795b88cb 2023-07-10 thomas return (strcmp(refname, wanted_ref) == 0);
1195 795b88cb 2023-07-10 thomas }
1196 795b88cb 2023-07-10 thomas
1197 795b88cb 2023-07-10 thomas static int
1198 795b88cb 2023-07-10 thomas is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1199 795b88cb 2023-07-10 thomas {
1200 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1201 795b88cb 2023-07-10 thomas
1202 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1203 795b88cb 2023-07-10 thomas if (match_wanted_ref(refname, pe->path))
1204 795b88cb 2023-07-10 thomas return 1;
1205 795b88cb 2023-07-10 thomas }
1206 795b88cb 2023-07-10 thomas
1207 795b88cb 2023-07-10 thomas return 0;
1208 795b88cb 2023-07-10 thomas }
1209 795b88cb 2023-07-10 thomas
1210 795b88cb 2023-07-10 thomas static const struct got_error *
1211 795b88cb 2023-07-10 thomas create_wanted_ref(const char *refname, struct got_object_id *id,
1212 795b88cb 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1213 795b88cb 2023-07-10 thomas {
1214 795b88cb 2023-07-10 thomas const struct got_error *err;
1215 795b88cb 2023-07-10 thomas char *remote_refname;
1216 795b88cb 2023-07-10 thomas
1217 795b88cb 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1218 795b88cb 2023-07-10 thomas refname += 5;
1219 795b88cb 2023-07-10 thomas
1220 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1221 795b88cb 2023-07-10 thomas remote_repo_name, refname) == -1)
1222 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
1223 795b88cb 2023-07-10 thomas
1224 795b88cb 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1225 795b88cb 2023-07-10 thomas free(remote_refname);
1226 795b88cb 2023-07-10 thomas return err;
1227 795b88cb 2023-07-10 thomas }
1228 795b88cb 2023-07-10 thomas
1229 795b88cb 2023-07-10 thomas static const struct got_error *
1230 795b88cb 2023-07-10 thomas create_gotconfig(const char *proto, const char *host, const char *port,
1231 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *default_branch,
1232 795b88cb 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1233 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1234 795b88cb 2023-07-10 thomas struct got_repository *repo)
1235 795b88cb 2023-07-10 thomas {
1236 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1237 795b88cb 2023-07-10 thomas char *gotconfig_path = NULL;
1238 795b88cb 2023-07-10 thomas char *gotconfig = NULL;
1239 795b88cb 2023-07-10 thomas FILE *gotconfig_file = NULL;
1240 795b88cb 2023-07-10 thomas const char *branchname = NULL;
1241 795b88cb 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1242 795b88cb 2023-07-10 thomas ssize_t n;
1243 795b88cb 2023-07-10 thomas
1244 795b88cb 2023-07-10 thomas if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1245 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1246 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1247 795b88cb 2023-07-10 thomas char *s;
1248 795b88cb 2023-07-10 thomas branchname = pe->path;
1249 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1250 795b88cb 2023-07-10 thomas branchname += 11;
1251 795b88cb 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1252 795b88cb 2023-07-10 thomas branches ? branches : "", branchname) == -1) {
1253 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1254 795b88cb 2023-07-10 thomas goto done;
1255 795b88cb 2023-07-10 thomas }
1256 795b88cb 2023-07-10 thomas free(branches);
1257 795b88cb 2023-07-10 thomas branches = s;
1258 795b88cb 2023-07-10 thomas }
1259 795b88cb 2023-07-10 thomas } else if (!fetch_all_branches && default_branch) {
1260 795b88cb 2023-07-10 thomas branchname = default_branch;
1261 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1262 795b88cb 2023-07-10 thomas branchname += 11;
1263 795b88cb 2023-07-10 thomas if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1264 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1265 795b88cb 2023-07-10 thomas goto done;
1266 795b88cb 2023-07-10 thomas }
1267 795b88cb 2023-07-10 thomas }
1268 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1269 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1270 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1271 795b88cb 2023-07-10 thomas char *s;
1272 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1273 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1274 795b88cb 2023-07-10 thomas branchname += 5;
1275 795b88cb 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1276 795b88cb 2023-07-10 thomas refs ? refs : "", refname) == -1) {
1277 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1278 795b88cb 2023-07-10 thomas goto done;
1279 795b88cb 2023-07-10 thomas }
1280 795b88cb 2023-07-10 thomas free(refs);
1281 795b88cb 2023-07-10 thomas refs = s;
1282 795b88cb 2023-07-10 thomas }
1283 795b88cb 2023-07-10 thomas }
1284 795b88cb 2023-07-10 thomas
1285 795b88cb 2023-07-10 thomas /* Create got.conf(5). */
1286 795b88cb 2023-07-10 thomas gotconfig_path = got_repo_get_path_gotconfig(repo);
1287 795b88cb 2023-07-10 thomas if (gotconfig_path == NULL) {
1288 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gotconfig");
1289 795b88cb 2023-07-10 thomas goto done;
1290 795b88cb 2023-07-10 thomas }
1291 795b88cb 2023-07-10 thomas gotconfig_file = fopen(gotconfig_path, "ae");
1292 795b88cb 2023-07-10 thomas if (gotconfig_file == NULL) {
1293 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fopen", gotconfig_path);
1294 795b88cb 2023-07-10 thomas goto done;
1295 795b88cb 2023-07-10 thomas }
1296 795b88cb 2023-07-10 thomas if (asprintf(&gotconfig,
1297 795b88cb 2023-07-10 thomas "remote \"%s\" {\n"
1298 795b88cb 2023-07-10 thomas "\tserver %s\n"
1299 795b88cb 2023-07-10 thomas "\tprotocol %s\n"
1300 795b88cb 2023-07-10 thomas "%s%s%s"
1301 795b88cb 2023-07-10 thomas "\trepository \"%s\"\n"
1302 795b88cb 2023-07-10 thomas "%s%s%s"
1303 795b88cb 2023-07-10 thomas "%s%s%s"
1304 795b88cb 2023-07-10 thomas "%s"
1305 795b88cb 2023-07-10 thomas "%s"
1306 795b88cb 2023-07-10 thomas "}\n",
1307 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1308 795b88cb 2023-07-10 thomas port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1309 795b88cb 2023-07-10 thomas remote_repo_path, branches ? "\tbranch { " : "",
1310 795b88cb 2023-07-10 thomas branches ? branches : "", branches ? "}\n" : "",
1311 795b88cb 2023-07-10 thomas refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1312 795b88cb 2023-07-10 thomas mirror_references ? "\tmirror_references yes\n" : "",
1313 795b88cb 2023-07-10 thomas fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1314 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1315 795b88cb 2023-07-10 thomas goto done;
1316 795b88cb 2023-07-10 thomas }
1317 795b88cb 2023-07-10 thomas n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1318 795b88cb 2023-07-10 thomas if (n != strlen(gotconfig)) {
1319 795b88cb 2023-07-10 thomas err = got_ferror(gotconfig_file, GOT_ERR_IO);
1320 795b88cb 2023-07-10 thomas goto done;
1321 795b88cb 2023-07-10 thomas }
1322 795b88cb 2023-07-10 thomas
1323 795b88cb 2023-07-10 thomas done:
1324 795b88cb 2023-07-10 thomas if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1325 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", gotconfig_path);
1326 795b88cb 2023-07-10 thomas free(gotconfig_path);
1327 795b88cb 2023-07-10 thomas free(branches);
1328 795b88cb 2023-07-10 thomas return err;
1329 795b88cb 2023-07-10 thomas }
1330 795b88cb 2023-07-10 thomas
1331 795b88cb 2023-07-10 thomas static const struct got_error *
1332 795b88cb 2023-07-10 thomas create_gitconfig(const char *git_url, const char *default_branch,
1333 795b88cb 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1334 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1335 795b88cb 2023-07-10 thomas struct got_repository *repo)
1336 795b88cb 2023-07-10 thomas {
1337 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1338 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL;
1339 795b88cb 2023-07-10 thomas char *gitconfig = NULL;
1340 795b88cb 2023-07-10 thomas FILE *gitconfig_file = NULL;
1341 795b88cb 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1342 795b88cb 2023-07-10 thomas const char *branchname;
1343 795b88cb 2023-07-10 thomas ssize_t n;
1344 795b88cb 2023-07-10 thomas
1345 795b88cb 2023-07-10 thomas /* Create a config file Git can understand. */
1346 795b88cb 2023-07-10 thomas gitconfig_path = got_repo_get_path_gitconfig(repo);
1347 795b88cb 2023-07-10 thomas if (gitconfig_path == NULL) {
1348 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gitconfig");
1349 795b88cb 2023-07-10 thomas goto done;
1350 795b88cb 2023-07-10 thomas }
1351 795b88cb 2023-07-10 thomas gitconfig_file = fopen(gitconfig_path, "ae");
1352 795b88cb 2023-07-10 thomas if (gitconfig_file == NULL) {
1353 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fopen", gitconfig_path);
1354 795b88cb 2023-07-10 thomas goto done;
1355 795b88cb 2023-07-10 thomas }
1356 795b88cb 2023-07-10 thomas if (fetch_all_branches) {
1357 795b88cb 2023-07-10 thomas if (mirror_references) {
1358 795b88cb 2023-07-10 thomas if (asprintf(&branches,
1359 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1360 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1361 795b88cb 2023-07-10 thomas goto done;
1362 795b88cb 2023-07-10 thomas }
1363 795b88cb 2023-07-10 thomas } else if (asprintf(&branches,
1364 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1365 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1366 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1367 795b88cb 2023-07-10 thomas goto done;
1368 795b88cb 2023-07-10 thomas }
1369 795b88cb 2023-07-10 thomas } else if (!TAILQ_EMPTY(wanted_branches)) {
1370 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1371 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1372 795b88cb 2023-07-10 thomas char *s;
1373 795b88cb 2023-07-10 thomas branchname = pe->path;
1374 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1375 795b88cb 2023-07-10 thomas branchname += 11;
1376 795b88cb 2023-07-10 thomas if (mirror_references) {
1377 795b88cb 2023-07-10 thomas if (asprintf(&s,
1378 795b88cb 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1379 795b88cb 2023-07-10 thomas branches ? branches : "",
1380 795b88cb 2023-07-10 thomas branchname, branchname) == -1) {
1381 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1382 795b88cb 2023-07-10 thomas goto done;
1383 795b88cb 2023-07-10 thomas }
1384 795b88cb 2023-07-10 thomas } else if (asprintf(&s,
1385 795b88cb 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1386 795b88cb 2023-07-10 thomas branches ? branches : "",
1387 795b88cb 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1388 795b88cb 2023-07-10 thomas branchname) == -1) {
1389 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1390 795b88cb 2023-07-10 thomas goto done;
1391 795b88cb 2023-07-10 thomas }
1392 795b88cb 2023-07-10 thomas free(branches);
1393 795b88cb 2023-07-10 thomas branches = s;
1394 795b88cb 2023-07-10 thomas }
1395 795b88cb 2023-07-10 thomas } else {
1396 795b88cb 2023-07-10 thomas /*
1397 795b88cb 2023-07-10 thomas * If the server specified a default branch, use just that one.
1398 795b88cb 2023-07-10 thomas * Otherwise fall back to fetching all branches on next fetch.
1399 795b88cb 2023-07-10 thomas */
1400 795b88cb 2023-07-10 thomas if (default_branch) {
1401 795b88cb 2023-07-10 thomas branchname = default_branch;
1402 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1403 795b88cb 2023-07-10 thomas branchname += 11;
1404 795b88cb 2023-07-10 thomas } else
1405 795b88cb 2023-07-10 thomas branchname = "*"; /* fall back to all branches */
1406 795b88cb 2023-07-10 thomas if (mirror_references) {
1407 795b88cb 2023-07-10 thomas if (asprintf(&branches,
1408 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/heads/%s\n",
1409 795b88cb 2023-07-10 thomas branchname, branchname) == -1) {
1410 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1411 795b88cb 2023-07-10 thomas goto done;
1412 795b88cb 2023-07-10 thomas }
1413 795b88cb 2023-07-10 thomas } else if (asprintf(&branches,
1414 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1415 795b88cb 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1416 795b88cb 2023-07-10 thomas branchname) == -1) {
1417 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1418 795b88cb 2023-07-10 thomas goto done;
1419 795b88cb 2023-07-10 thomas }
1420 795b88cb 2023-07-10 thomas }
1421 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1422 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1423 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1424 795b88cb 2023-07-10 thomas char *s;
1425 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1426 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1427 795b88cb 2023-07-10 thomas refname += 5;
1428 795b88cb 2023-07-10 thomas if (mirror_references) {
1429 795b88cb 2023-07-10 thomas if (asprintf(&s,
1430 795b88cb 2023-07-10 thomas "%s\tfetch = refs/%s:refs/%s\n",
1431 795b88cb 2023-07-10 thomas refs ? refs : "", refname, refname) == -1) {
1432 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1433 795b88cb 2023-07-10 thomas goto done;
1434 795b88cb 2023-07-10 thomas }
1435 795b88cb 2023-07-10 thomas } else if (asprintf(&s,
1436 795b88cb 2023-07-10 thomas "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1437 795b88cb 2023-07-10 thomas refs ? refs : "",
1438 795b88cb 2023-07-10 thomas refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1439 795b88cb 2023-07-10 thomas refname) == -1) {
1440 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1441 795b88cb 2023-07-10 thomas goto done;
1442 795b88cb 2023-07-10 thomas }
1443 795b88cb 2023-07-10 thomas free(refs);
1444 795b88cb 2023-07-10 thomas refs = s;
1445 795b88cb 2023-07-10 thomas }
1446 795b88cb 2023-07-10 thomas }
1447 795b88cb 2023-07-10 thomas
1448 795b88cb 2023-07-10 thomas if (asprintf(&gitconfig,
1449 795b88cb 2023-07-10 thomas "[remote \"%s\"]\n"
1450 795b88cb 2023-07-10 thomas "\turl = %s\n"
1451 795b88cb 2023-07-10 thomas "%s"
1452 795b88cb 2023-07-10 thomas "%s"
1453 795b88cb 2023-07-10 thomas "\tfetch = refs/tags/*:refs/tags/*\n",
1454 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1455 795b88cb 2023-07-10 thomas refs ? refs : "") == -1) {
1456 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1457 795b88cb 2023-07-10 thomas goto done;
1458 795b88cb 2023-07-10 thomas }
1459 795b88cb 2023-07-10 thomas n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1460 795b88cb 2023-07-10 thomas if (n != strlen(gitconfig)) {
1461 795b88cb 2023-07-10 thomas err = got_ferror(gitconfig_file, GOT_ERR_IO);
1462 795b88cb 2023-07-10 thomas goto done;
1463 795b88cb 2023-07-10 thomas }
1464 795b88cb 2023-07-10 thomas done:
1465 795b88cb 2023-07-10 thomas if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1466 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", gitconfig_path);
1467 795b88cb 2023-07-10 thomas free(gitconfig_path);
1468 795b88cb 2023-07-10 thomas free(branches);
1469 795b88cb 2023-07-10 thomas return err;
1470 795b88cb 2023-07-10 thomas }
1471 795b88cb 2023-07-10 thomas
1472 795b88cb 2023-07-10 thomas static const struct got_error *
1473 795b88cb 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1474 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1475 795b88cb 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1476 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1477 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1478 795b88cb 2023-07-10 thomas {
1479 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1480 795b88cb 2023-07-10 thomas const char *default_branch = NULL;
1481 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1482 795b88cb 2023-07-10 thomas
1483 795b88cb 2023-07-10 thomas /*
1484 795b88cb 2023-07-10 thomas * If we asked for a set of wanted branches then use the first
1485 795b88cb 2023-07-10 thomas * one of those.
1486 795b88cb 2023-07-10 thomas */
1487 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_branches)) {
1488 795b88cb 2023-07-10 thomas pe = TAILQ_FIRST(wanted_branches);
1489 795b88cb 2023-07-10 thomas default_branch = pe->path;
1490 795b88cb 2023-07-10 thomas } else {
1491 795b88cb 2023-07-10 thomas /* First HEAD ref listed by server is the default branch. */
1492 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1493 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1494 795b88cb 2023-07-10 thomas const char *target = pe->data;
1495 795b88cb 2023-07-10 thomas
1496 795b88cb 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1497 795b88cb 2023-07-10 thomas continue;
1498 795b88cb 2023-07-10 thomas
1499 795b88cb 2023-07-10 thomas default_branch = target;
1500 795b88cb 2023-07-10 thomas break;
1501 795b88cb 2023-07-10 thomas }
1502 795b88cb 2023-07-10 thomas }
1503 795b88cb 2023-07-10 thomas
1504 795b88cb 2023-07-10 thomas /* Create got.conf(5). */
1505 795b88cb 2023-07-10 thomas err = create_gotconfig(proto, host, port, remote_repo_path,
1506 795b88cb 2023-07-10 thomas default_branch, fetch_all_branches, wanted_branches,
1507 795b88cb 2023-07-10 thomas wanted_refs, mirror_references, repo);
1508 795b88cb 2023-07-10 thomas if (err)
1509 795b88cb 2023-07-10 thomas return err;
1510 795b88cb 2023-07-10 thomas
1511 795b88cb 2023-07-10 thomas /* Create a config file Git can understand. */
1512 795b88cb 2023-07-10 thomas return create_gitconfig(git_url, default_branch, fetch_all_branches,
1513 795b88cb 2023-07-10 thomas wanted_branches, wanted_refs, mirror_references, repo);
1514 795b88cb 2023-07-10 thomas }
1515 795b88cb 2023-07-10 thomas
1516 795b88cb 2023-07-10 thomas static const struct got_error *
1517 795b88cb 2023-07-10 thomas cmd_clone(int argc, char *argv[])
1518 795b88cb 2023-07-10 thomas {
1519 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
1520 795b88cb 2023-07-10 thomas const char *uri, *dirname;
1521 795b88cb 2023-07-10 thomas char *proto, *host, *port, *repo_name, *server_path;
1522 795b88cb 2023-07-10 thomas char *default_destdir = NULL, *id_str = NULL;
1523 795b88cb 2023-07-10 thomas const char *repo_path;
1524 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
1525 795b88cb 2023-07-10 thomas struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1526 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1527 795b88cb 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
1528 795b88cb 2023-07-10 thomas int ch, fetchfd = -1, fetchstatus;
1529 795b88cb 2023-07-10 thomas pid_t fetchpid = -1;
1530 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg fpa;
1531 795b88cb 2023-07-10 thomas char *git_url = NULL;
1532 795b88cb 2023-07-10 thomas int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1533 795b88cb 2023-07-10 thomas int bflag = 0, list_refs_only = 0;
1534 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
1535 795b88cb 2023-07-10 thomas
1536 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
1537 795b88cb 2023-07-10 thomas TAILQ_INIT(&symrefs);
1538 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
1539 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
1540 795b88cb 2023-07-10 thomas
1541 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1542 795b88cb 2023-07-10 thomas switch (ch) {
1543 795b88cb 2023-07-10 thomas case 'a':
1544 795b88cb 2023-07-10 thomas fetch_all_branches = 1;
1545 795b88cb 2023-07-10 thomas break;
1546 795b88cb 2023-07-10 thomas case 'b':
1547 795b88cb 2023-07-10 thomas error = got_pathlist_append(&wanted_branches,
1548 795b88cb 2023-07-10 thomas optarg, NULL);
1549 795b88cb 2023-07-10 thomas if (error)
1550 795b88cb 2023-07-10 thomas return error;
1551 795b88cb 2023-07-10 thomas bflag = 1;
1552 795b88cb 2023-07-10 thomas break;
1553 795b88cb 2023-07-10 thomas case 'l':
1554 795b88cb 2023-07-10 thomas list_refs_only = 1;
1555 795b88cb 2023-07-10 thomas break;
1556 795b88cb 2023-07-10 thomas case 'm':
1557 795b88cb 2023-07-10 thomas mirror_references = 1;
1558 795b88cb 2023-07-10 thomas break;
1559 795b88cb 2023-07-10 thomas case 'q':
1560 795b88cb 2023-07-10 thomas verbosity = -1;
1561 795b88cb 2023-07-10 thomas break;
1562 795b88cb 2023-07-10 thomas case 'R':
1563 795b88cb 2023-07-10 thomas error = got_pathlist_append(&wanted_refs,
1564 795b88cb 2023-07-10 thomas optarg, NULL);
1565 795b88cb 2023-07-10 thomas if (error)
1566 795b88cb 2023-07-10 thomas return error;
1567 795b88cb 2023-07-10 thomas break;
1568 795b88cb 2023-07-10 thomas case 'v':
1569 795b88cb 2023-07-10 thomas if (verbosity < 0)
1570 795b88cb 2023-07-10 thomas verbosity = 0;
1571 795b88cb 2023-07-10 thomas else if (verbosity < 3)
1572 795b88cb 2023-07-10 thomas verbosity++;
1573 795b88cb 2023-07-10 thomas break;
1574 795b88cb 2023-07-10 thomas default:
1575 795b88cb 2023-07-10 thomas usage_clone();
1576 795b88cb 2023-07-10 thomas break;
1577 795b88cb 2023-07-10 thomas }
1578 795b88cb 2023-07-10 thomas }
1579 795b88cb 2023-07-10 thomas argc -= optind;
1580 795b88cb 2023-07-10 thomas argv += optind;
1581 795b88cb 2023-07-10 thomas
1582 795b88cb 2023-07-10 thomas if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1583 795b88cb 2023-07-10 thomas option_conflict('a', 'b');
1584 795b88cb 2023-07-10 thomas if (list_refs_only) {
1585 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_branches))
1586 795b88cb 2023-07-10 thomas option_conflict('l', 'b');
1587 795b88cb 2023-07-10 thomas if (fetch_all_branches)
1588 795b88cb 2023-07-10 thomas option_conflict('l', 'a');
1589 795b88cb 2023-07-10 thomas if (mirror_references)
1590 795b88cb 2023-07-10 thomas option_conflict('l', 'm');
1591 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_refs))
1592 795b88cb 2023-07-10 thomas option_conflict('l', 'R');
1593 795b88cb 2023-07-10 thomas }
1594 795b88cb 2023-07-10 thomas
1595 795b88cb 2023-07-10 thomas uri = argv[0];
1596 795b88cb 2023-07-10 thomas
1597 795b88cb 2023-07-10 thomas if (argc == 1)
1598 795b88cb 2023-07-10 thomas dirname = NULL;
1599 795b88cb 2023-07-10 thomas else if (argc == 2)
1600 795b88cb 2023-07-10 thomas dirname = argv[1];
1601 795b88cb 2023-07-10 thomas else
1602 795b88cb 2023-07-10 thomas usage_clone();
1603 795b88cb 2023-07-10 thomas
1604 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1605 795b88cb 2023-07-10 thomas &repo_name, uri);
1606 795b88cb 2023-07-10 thomas if (error)
1607 795b88cb 2023-07-10 thomas goto done;
1608 795b88cb 2023-07-10 thomas
1609 795b88cb 2023-07-10 thomas if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1610 795b88cb 2023-07-10 thomas host, port ? ":" : "", port ? port : "",
1611 795b88cb 2023-07-10 thomas server_path[0] != '/' ? "/" : "", server_path) == -1) {
1612 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1613 795b88cb 2023-07-10 thomas goto done;
1614 795b88cb 2023-07-10 thomas }
1615 795b88cb 2023-07-10 thomas
1616 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
1617 795b88cb 2023-07-10 thomas #ifndef PROFILE
1618 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1619 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
1620 795b88cb 2023-07-10 thomas err(1, "pledge");
1621 795b88cb 2023-07-10 thomas #endif
1622 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
1623 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
1624 795b88cb 2023-07-10 thomas #ifndef PROFILE
1625 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1626 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
1627 795b88cb 2023-07-10 thomas err(1, "pledge");
1628 795b88cb 2023-07-10 thomas #endif
1629 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
1630 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
1631 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1632 795b88cb 2023-07-10 thomas goto done;
1633 795b88cb 2023-07-10 thomas } else {
1634 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1635 795b88cb 2023-07-10 thomas goto done;
1636 795b88cb 2023-07-10 thomas }
1637 795b88cb 2023-07-10 thomas if (dirname == NULL) {
1638 795b88cb 2023-07-10 thomas if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1639 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1640 795b88cb 2023-07-10 thomas goto done;
1641 795b88cb 2023-07-10 thomas }
1642 795b88cb 2023-07-10 thomas repo_path = default_destdir;
1643 795b88cb 2023-07-10 thomas } else
1644 795b88cb 2023-07-10 thomas repo_path = dirname;
1645 795b88cb 2023-07-10 thomas
1646 795b88cb 2023-07-10 thomas if (!list_refs_only) {
1647 795b88cb 2023-07-10 thomas error = got_path_mkdir(repo_path);
1648 795b88cb 2023-07-10 thomas if (error &&
1649 795b88cb 2023-07-10 thomas (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1650 795b88cb 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1651 795b88cb 2023-07-10 thomas goto done;
1652 795b88cb 2023-07-10 thomas if (!got_path_dir_is_empty(repo_path)) {
1653 795b88cb 2023-07-10 thomas error = got_error_path(repo_path,
1654 795b88cb 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
1655 795b88cb 2023-07-10 thomas goto done;
1656 795b88cb 2023-07-10 thomas }
1657 795b88cb 2023-07-10 thomas }
1658 795b88cb 2023-07-10 thomas
1659 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
1660 795b88cb 2023-07-10 thomas if (error)
1661 795b88cb 2023-07-10 thomas goto done;
1662 795b88cb 2023-07-10 thomas
1663 795b88cb 2023-07-10 thomas error = apply_unveil(repo_path, 0, NULL);
1664 795b88cb 2023-07-10 thomas if (error)
1665 795b88cb 2023-07-10 thomas goto done;
1666 795b88cb 2023-07-10 thomas
1667 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1668 795b88cb 2023-07-10 thomas printf("Connecting to %s\n", git_url);
1669 795b88cb 2023-07-10 thomas
1670 795b88cb 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1671 795b88cb 2023-07-10 thomas server_path, verbosity);
1672 795b88cb 2023-07-10 thomas if (error)
1673 795b88cb 2023-07-10 thomas goto done;
1674 795b88cb 2023-07-10 thomas
1675 795b88cb 2023-07-10 thomas if (!list_refs_only) {
1676 795b88cb 2023-07-10 thomas error = got_repo_init(repo_path, NULL);
1677 795b88cb 2023-07-10 thomas if (error)
1678 795b88cb 2023-07-10 thomas goto done;
1679 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1680 795b88cb 2023-07-10 thomas if (error != NULL)
1681 795b88cb 2023-07-10 thomas goto done;
1682 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1683 795b88cb 2023-07-10 thomas if (error)
1684 795b88cb 2023-07-10 thomas goto done;
1685 795b88cb 2023-07-10 thomas }
1686 795b88cb 2023-07-10 thomas
1687 795b88cb 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
1688 795b88cb 2023-07-10 thomas fpa.last_p_indexed = -1;
1689 795b88cb 2023-07-10 thomas fpa.last_p_resolved = -1;
1690 795b88cb 2023-07-10 thomas fpa.verbosity = verbosity;
1691 795b88cb 2023-07-10 thomas fpa.create_configs = 1;
1692 795b88cb 2023-07-10 thomas fpa.configs_created = 0;
1693 795b88cb 2023-07-10 thomas fpa.repo = repo;
1694 795b88cb 2023-07-10 thomas fpa.config_info.symrefs = &symrefs;
1695 795b88cb 2023-07-10 thomas fpa.config_info.wanted_branches = &wanted_branches;
1696 795b88cb 2023-07-10 thomas fpa.config_info.wanted_refs = &wanted_refs;
1697 795b88cb 2023-07-10 thomas fpa.config_info.proto = proto;
1698 795b88cb 2023-07-10 thomas fpa.config_info.host = host;
1699 795b88cb 2023-07-10 thomas fpa.config_info.port = port;
1700 795b88cb 2023-07-10 thomas fpa.config_info.remote_repo_path = server_path;
1701 795b88cb 2023-07-10 thomas fpa.config_info.git_url = git_url;
1702 795b88cb 2023-07-10 thomas fpa.config_info.fetch_all_branches = fetch_all_branches;
1703 795b88cb 2023-07-10 thomas fpa.config_info.mirror_references = mirror_references;
1704 795b88cb 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1705 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1706 795b88cb 2023-07-10 thomas fetch_all_branches, &wanted_branches, &wanted_refs,
1707 795b88cb 2023-07-10 thomas list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1708 795b88cb 2023-07-10 thomas fetch_progress, &fpa);
1709 795b88cb 2023-07-10 thomas if (error)
1710 795b88cb 2023-07-10 thomas goto done;
1711 795b88cb 2023-07-10 thomas
1712 795b88cb 2023-07-10 thomas if (list_refs_only) {
1713 795b88cb 2023-07-10 thomas error = list_remote_refs(&symrefs, &refs);
1714 795b88cb 2023-07-10 thomas goto done;
1715 795b88cb 2023-07-10 thomas }
1716 795b88cb 2023-07-10 thomas
1717 795b88cb 2023-07-10 thomas if (pack_hash == NULL) {
1718 795b88cb 2023-07-10 thomas error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1719 795b88cb 2023-07-10 thomas "server sent an empty pack file");
1720 795b88cb 2023-07-10 thomas goto done;
1721 795b88cb 2023-07-10 thomas }
1722 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
1723 795b88cb 2023-07-10 thomas if (error)
1724 795b88cb 2023-07-10 thomas goto done;
1725 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1726 795b88cb 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
1727 795b88cb 2023-07-10 thomas free(id_str);
1728 795b88cb 2023-07-10 thomas
1729 795b88cb 2023-07-10 thomas /* Set up references provided with the pack file. */
1730 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
1731 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1732 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
1733 795b88cb 2023-07-10 thomas char *remote_refname;
1734 795b88cb 2023-07-10 thomas
1735 795b88cb 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname) &&
1736 795b88cb 2023-07-10 thomas !mirror_references) {
1737 795b88cb 2023-07-10 thomas error = create_wanted_ref(refname, id,
1738 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME,
1739 795b88cb 2023-07-10 thomas verbosity - 1, repo);
1740 795b88cb 2023-07-10 thomas if (error)
1741 795b88cb 2023-07-10 thomas goto done;
1742 795b88cb 2023-07-10 thomas continue;
1743 795b88cb 2023-07-10 thomas }
1744 795b88cb 2023-07-10 thomas
1745 795b88cb 2023-07-10 thomas error = create_ref(refname, id, verbosity - 1, repo);
1746 795b88cb 2023-07-10 thomas if (error)
1747 795b88cb 2023-07-10 thomas goto done;
1748 795b88cb 2023-07-10 thomas
1749 795b88cb 2023-07-10 thomas if (mirror_references)
1750 795b88cb 2023-07-10 thomas continue;
1751 795b88cb 2023-07-10 thomas
1752 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", refname, 11) != 0)
1753 795b88cb 2023-07-10 thomas continue;
1754 795b88cb 2023-07-10 thomas
1755 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname,
1756 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1757 795b88cb 2023-07-10 thomas refname + 11) == -1) {
1758 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1759 795b88cb 2023-07-10 thomas goto done;
1760 795b88cb 2023-07-10 thomas }
1761 795b88cb 2023-07-10 thomas error = create_ref(remote_refname, id, verbosity - 1, repo);
1762 795b88cb 2023-07-10 thomas free(remote_refname);
1763 795b88cb 2023-07-10 thomas if (error)
1764 795b88cb 2023-07-10 thomas goto done;
1765 795b88cb 2023-07-10 thomas }
1766 795b88cb 2023-07-10 thomas
1767 795b88cb 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
1768 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
1769 795b88cb 2023-07-10 thomas struct got_reference *target_ref;
1770 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1771 795b88cb 2023-07-10 thomas const char *target = pe->data;
1772 795b88cb 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
1773 795b88cb 2023-07-10 thomas
1774 795b88cb 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1775 795b88cb 2023-07-10 thomas continue;
1776 795b88cb 2023-07-10 thomas
1777 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1778 795b88cb 2023-07-10 thomas if (error) {
1779 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1780 795b88cb 2023-07-10 thomas error = NULL;
1781 795b88cb 2023-07-10 thomas continue;
1782 795b88cb 2023-07-10 thomas }
1783 795b88cb 2023-07-10 thomas goto done;
1784 795b88cb 2023-07-10 thomas }
1785 795b88cb 2023-07-10 thomas
1786 795b88cb 2023-07-10 thomas error = create_symref(refname, target_ref, verbosity, repo);
1787 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1788 795b88cb 2023-07-10 thomas if (error)
1789 795b88cb 2023-07-10 thomas goto done;
1790 795b88cb 2023-07-10 thomas
1791 795b88cb 2023-07-10 thomas if (mirror_references)
1792 795b88cb 2023-07-10 thomas continue;
1793 795b88cb 2023-07-10 thomas
1794 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
1795 795b88cb 2023-07-10 thomas continue;
1796 795b88cb 2023-07-10 thomas
1797 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname,
1798 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1799 795b88cb 2023-07-10 thomas refname) == -1) {
1800 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1801 795b88cb 2023-07-10 thomas goto done;
1802 795b88cb 2023-07-10 thomas }
1803 795b88cb 2023-07-10 thomas if (asprintf(&remote_target,
1804 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1805 795b88cb 2023-07-10 thomas target + 11) == -1) {
1806 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1807 795b88cb 2023-07-10 thomas free(remote_refname);
1808 795b88cb 2023-07-10 thomas goto done;
1809 795b88cb 2023-07-10 thomas }
1810 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, remote_target, 0);
1811 795b88cb 2023-07-10 thomas if (error) {
1812 795b88cb 2023-07-10 thomas free(remote_refname);
1813 795b88cb 2023-07-10 thomas free(remote_target);
1814 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1815 795b88cb 2023-07-10 thomas error = NULL;
1816 795b88cb 2023-07-10 thomas continue;
1817 795b88cb 2023-07-10 thomas }
1818 795b88cb 2023-07-10 thomas goto done;
1819 795b88cb 2023-07-10 thomas }
1820 795b88cb 2023-07-10 thomas error = create_symref(remote_refname, target_ref,
1821 795b88cb 2023-07-10 thomas verbosity - 1, repo);
1822 795b88cb 2023-07-10 thomas free(remote_refname);
1823 795b88cb 2023-07-10 thomas free(remote_target);
1824 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1825 795b88cb 2023-07-10 thomas if (error)
1826 795b88cb 2023-07-10 thomas goto done;
1827 795b88cb 2023-07-10 thomas }
1828 795b88cb 2023-07-10 thomas if (pe == NULL) {
1829 795b88cb 2023-07-10 thomas /*
1830 795b88cb 2023-07-10 thomas * We failed to set the HEAD reference. If we asked for
1831 795b88cb 2023-07-10 thomas * a set of wanted branches use the first of one of those
1832 795b88cb 2023-07-10 thomas * which could be fetched instead.
1833 795b88cb 2023-07-10 thomas */
1834 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &wanted_branches, entry) {
1835 795b88cb 2023-07-10 thomas const char *target = pe->path;
1836 795b88cb 2023-07-10 thomas struct got_reference *target_ref;
1837 795b88cb 2023-07-10 thomas
1838 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1839 795b88cb 2023-07-10 thomas if (error) {
1840 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1841 795b88cb 2023-07-10 thomas error = NULL;
1842 795b88cb 2023-07-10 thomas continue;
1843 795b88cb 2023-07-10 thomas }
1844 795b88cb 2023-07-10 thomas goto done;
1845 795b88cb 2023-07-10 thomas }
1846 795b88cb 2023-07-10 thomas
1847 795b88cb 2023-07-10 thomas error = create_symref(GOT_REF_HEAD, target_ref,
1848 795b88cb 2023-07-10 thomas verbosity, repo);
1849 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1850 795b88cb 2023-07-10 thomas if (error)
1851 795b88cb 2023-07-10 thomas goto done;
1852 795b88cb 2023-07-10 thomas break;
1853 795b88cb 2023-07-10 thomas }
1854 795b88cb 2023-07-10 thomas
1855 795b88cb 2023-07-10 thomas if (!fpa.configs_created && pe != NULL) {
1856 795b88cb 2023-07-10 thomas error = create_config_files(fpa.config_info.proto,
1857 795b88cb 2023-07-10 thomas fpa.config_info.host, fpa.config_info.port,
1858 795b88cb 2023-07-10 thomas fpa.config_info.remote_repo_path,
1859 795b88cb 2023-07-10 thomas fpa.config_info.git_url,
1860 795b88cb 2023-07-10 thomas fpa.config_info.fetch_all_branches,
1861 795b88cb 2023-07-10 thomas fpa.config_info.mirror_references,
1862 795b88cb 2023-07-10 thomas fpa.config_info.symrefs,
1863 795b88cb 2023-07-10 thomas fpa.config_info.wanted_branches,
1864 795b88cb 2023-07-10 thomas fpa.config_info.wanted_refs, fpa.repo);
1865 795b88cb 2023-07-10 thomas if (error)
1866 795b88cb 2023-07-10 thomas goto done;
1867 795b88cb 2023-07-10 thomas }
1868 795b88cb 2023-07-10 thomas }
1869 795b88cb 2023-07-10 thomas
1870 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1871 795b88cb 2023-07-10 thomas printf("Created %s repository '%s'\n",
1872 795b88cb 2023-07-10 thomas mirror_references ? "mirrored" : "cloned", repo_path);
1873 795b88cb 2023-07-10 thomas done:
1874 795b88cb 2023-07-10 thomas if (pack_fds) {
1875 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
1876 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
1877 795b88cb 2023-07-10 thomas if (error == NULL)
1878 795b88cb 2023-07-10 thomas error = pack_err;
1879 795b88cb 2023-07-10 thomas }
1880 795b88cb 2023-07-10 thomas if (fetchpid > 0) {
1881 795b88cb 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
1882 795b88cb 2023-07-10 thomas error = got_error_from_errno("kill");
1883 795b88cb 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1884 795b88cb 2023-07-10 thomas error = got_error_from_errno("waitpid");
1885 795b88cb 2023-07-10 thomas }
1886 795b88cb 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1887 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
1888 795b88cb 2023-07-10 thomas if (repo) {
1889 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
1890 795b88cb 2023-07-10 thomas if (error == NULL)
1891 795b88cb 2023-07-10 thomas error = close_err;
1892 795b88cb 2023-07-10 thomas }
1893 795b88cb 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1894 795b88cb 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1895 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1896 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1897 795b88cb 2023-07-10 thomas free(pack_hash);
1898 795b88cb 2023-07-10 thomas free(proto);
1899 795b88cb 2023-07-10 thomas free(host);
1900 795b88cb 2023-07-10 thomas free(port);
1901 795b88cb 2023-07-10 thomas free(server_path);
1902 795b88cb 2023-07-10 thomas free(repo_name);
1903 795b88cb 2023-07-10 thomas free(default_destdir);
1904 795b88cb 2023-07-10 thomas free(git_url);
1905 795b88cb 2023-07-10 thomas return error;
1906 795b88cb 2023-07-10 thomas }
1907 795b88cb 2023-07-10 thomas
1908 795b88cb 2023-07-10 thomas static const struct got_error *
1909 795b88cb 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
1910 795b88cb 2023-07-10 thomas int replace_tags, int verbosity, struct got_repository *repo)
1911 795b88cb 2023-07-10 thomas {
1912 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1913 795b88cb 2023-07-10 thomas char *new_id_str = NULL;
1914 795b88cb 2023-07-10 thomas struct got_object_id *old_id = NULL;
1915 795b88cb 2023-07-10 thomas
1916 795b88cb 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
1917 795b88cb 2023-07-10 thomas if (err)
1918 795b88cb 2023-07-10 thomas goto done;
1919 795b88cb 2023-07-10 thomas
1920 795b88cb 2023-07-10 thomas if (!replace_tags &&
1921 795b88cb 2023-07-10 thomas strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1922 795b88cb 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1923 795b88cb 2023-07-10 thomas if (err)
1924 795b88cb 2023-07-10 thomas goto done;
1925 795b88cb 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1926 795b88cb 2023-07-10 thomas goto done;
1927 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
1928 795b88cb 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
1929 795b88cb 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
1930 795b88cb 2023-07-10 thomas }
1931 795b88cb 2023-07-10 thomas goto done;
1932 795b88cb 2023-07-10 thomas }
1933 795b88cb 2023-07-10 thomas
1934 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
1935 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
1936 795b88cb 2023-07-10 thomas printf("Replacing reference %s: %s\n",
1937 795b88cb 2023-07-10 thomas got_ref_get_name(ref),
1938 795b88cb 2023-07-10 thomas got_ref_get_symref_target(ref));
1939 795b88cb 2023-07-10 thomas }
1940 795b88cb 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
1941 795b88cb 2023-07-10 thomas if (err)
1942 795b88cb 2023-07-10 thomas goto done;
1943 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1944 795b88cb 2023-07-10 thomas if (err)
1945 795b88cb 2023-07-10 thomas goto done;
1946 795b88cb 2023-07-10 thomas } else {
1947 795b88cb 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1948 795b88cb 2023-07-10 thomas if (err)
1949 795b88cb 2023-07-10 thomas goto done;
1950 795b88cb 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1951 795b88cb 2023-07-10 thomas goto done;
1952 795b88cb 2023-07-10 thomas
1953 795b88cb 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
1954 795b88cb 2023-07-10 thomas if (err)
1955 795b88cb 2023-07-10 thomas goto done;
1956 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1957 795b88cb 2023-07-10 thomas if (err)
1958 795b88cb 2023-07-10 thomas goto done;
1959 795b88cb 2023-07-10 thomas }
1960 795b88cb 2023-07-10 thomas
1961 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1962 795b88cb 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
1963 795b88cb 2023-07-10 thomas new_id_str);
1964 795b88cb 2023-07-10 thomas done:
1965 795b88cb 2023-07-10 thomas free(old_id);
1966 795b88cb 2023-07-10 thomas free(new_id_str);
1967 795b88cb 2023-07-10 thomas return err;
1968 795b88cb 2023-07-10 thomas }
1969 795b88cb 2023-07-10 thomas
1970 795b88cb 2023-07-10 thomas static const struct got_error *
1971 795b88cb 2023-07-10 thomas update_wanted_ref(const char *refname, struct got_object_id *id,
1972 795b88cb 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1973 795b88cb 2023-07-10 thomas {
1974 795b88cb 2023-07-10 thomas const struct got_error *err, *unlock_err;
1975 795b88cb 2023-07-10 thomas char *remote_refname;
1976 795b88cb 2023-07-10 thomas struct got_reference *ref;
1977 795b88cb 2023-07-10 thomas
1978 795b88cb 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1979 795b88cb 2023-07-10 thomas refname += 5;
1980 795b88cb 2023-07-10 thomas
1981 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1982 795b88cb 2023-07-10 thomas remote_repo_name, refname) == -1)
1983 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
1984 795b88cb 2023-07-10 thomas
1985 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, remote_refname, 1);
1986 795b88cb 2023-07-10 thomas if (err) {
1987 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
1988 795b88cb 2023-07-10 thomas goto done;
1989 795b88cb 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1990 795b88cb 2023-07-10 thomas } else {
1991 795b88cb 2023-07-10 thomas err = update_ref(ref, id, 0, verbosity, repo);
1992 795b88cb 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
1993 795b88cb 2023-07-10 thomas if (unlock_err && err == NULL)
1994 795b88cb 2023-07-10 thomas err = unlock_err;
1995 795b88cb 2023-07-10 thomas got_ref_close(ref);
1996 795b88cb 2023-07-10 thomas }
1997 795b88cb 2023-07-10 thomas done:
1998 795b88cb 2023-07-10 thomas free(remote_refname);
1999 795b88cb 2023-07-10 thomas return err;
2000 795b88cb 2023-07-10 thomas }
2001 795b88cb 2023-07-10 thomas
2002 795b88cb 2023-07-10 thomas __dead static void
2003 795b88cb 2023-07-10 thomas usage_checkout(void)
2004 795b88cb 2023-07-10 thomas {
2005 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2006 795b88cb 2023-07-10 thomas "[-p path-prefix] repository-path [work-tree-path]\n",
2007 795b88cb 2023-07-10 thomas getprogname());
2008 795b88cb 2023-07-10 thomas exit(1);
2009 795b88cb 2023-07-10 thomas }
2010 795b88cb 2023-07-10 thomas
2011 795b88cb 2023-07-10 thomas static void
2012 795b88cb 2023-07-10 thomas show_worktree_base_ref_warning(void)
2013 795b88cb 2023-07-10 thomas {
2014 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: warning: could not create a reference "
2015 795b88cb 2023-07-10 thomas "to the work tree's base commit; the commit could be "
2016 795b88cb 2023-07-10 thomas "garbage-collected by Git or 'gotadmin cleanup'; making the "
2017 795b88cb 2023-07-10 thomas "repository writable and running 'got update' will prevent this\n",
2018 795b88cb 2023-07-10 thomas getprogname());
2019 795b88cb 2023-07-10 thomas }
2020 795b88cb 2023-07-10 thomas
2021 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg {
2022 795b88cb 2023-07-10 thomas const char *worktree_path;
2023 795b88cb 2023-07-10 thomas int had_base_commit_ref_error;
2024 795b88cb 2023-07-10 thomas int verbosity;
2025 795b88cb 2023-07-10 thomas };
2026 795b88cb 2023-07-10 thomas
2027 795b88cb 2023-07-10 thomas static const struct got_error *
2028 795b88cb 2023-07-10 thomas checkout_progress(void *arg, unsigned char status, const char *path)
2029 795b88cb 2023-07-10 thomas {
2030 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg *a = arg;
2031 795b88cb 2023-07-10 thomas
2032 795b88cb 2023-07-10 thomas /* Base commit bump happens silently. */
2033 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2034 795b88cb 2023-07-10 thomas return NULL;
2035 795b88cb 2023-07-10 thomas
2036 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BASE_REF_ERR) {
2037 795b88cb 2023-07-10 thomas a->had_base_commit_ref_error = 1;
2038 795b88cb 2023-07-10 thomas return NULL;
2039 795b88cb 2023-07-10 thomas }
2040 795b88cb 2023-07-10 thomas
2041 795b88cb 2023-07-10 thomas while (path[0] == '/')
2042 795b88cb 2023-07-10 thomas path++;
2043 795b88cb 2023-07-10 thomas
2044 795b88cb 2023-07-10 thomas if (a->verbosity >= 0)
2045 795b88cb 2023-07-10 thomas printf("%c %s/%s\n", status, a->worktree_path, path);
2046 795b88cb 2023-07-10 thomas
2047 795b88cb 2023-07-10 thomas return NULL;
2048 795b88cb 2023-07-10 thomas }
2049 795b88cb 2023-07-10 thomas
2050 795b88cb 2023-07-10 thomas static const struct got_error *
2051 795b88cb 2023-07-10 thomas check_cancelled(void *arg)
2052 795b88cb 2023-07-10 thomas {
2053 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
2054 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2055 795b88cb 2023-07-10 thomas return NULL;
2056 795b88cb 2023-07-10 thomas }
2057 795b88cb 2023-07-10 thomas
2058 795b88cb 2023-07-10 thomas static const struct got_error *
2059 795b88cb 2023-07-10 thomas check_linear_ancestry(struct got_object_id *commit_id,
2060 795b88cb 2023-07-10 thomas struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2061 795b88cb 2023-07-10 thomas struct got_repository *repo)
2062 795b88cb 2023-07-10 thomas {
2063 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2064 795b88cb 2023-07-10 thomas struct got_object_id *yca_id;
2065 795b88cb 2023-07-10 thomas
2066 795b88cb 2023-07-10 thomas err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2067 807c60e9 2024-03-30 thomas commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2068 795b88cb 2023-07-10 thomas if (err)
2069 795b88cb 2023-07-10 thomas return err;
2070 795b88cb 2023-07-10 thomas
2071 795b88cb 2023-07-10 thomas if (yca_id == NULL)
2072 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2073 795b88cb 2023-07-10 thomas
2074 795b88cb 2023-07-10 thomas /*
2075 795b88cb 2023-07-10 thomas * Require a straight line of history between the target commit
2076 795b88cb 2023-07-10 thomas * and the work tree's base commit.
2077 795b88cb 2023-07-10 thomas *
2078 795b88cb 2023-07-10 thomas * Non-linear situations such as this require a rebase:
2079 795b88cb 2023-07-10 thomas *
2080 795b88cb 2023-07-10 thomas * (commit) D F (base_commit)
2081 795b88cb 2023-07-10 thomas * \ /
2082 795b88cb 2023-07-10 thomas * C E
2083 795b88cb 2023-07-10 thomas * \ /
2084 795b88cb 2023-07-10 thomas * B (yca)
2085 795b88cb 2023-07-10 thomas * |
2086 795b88cb 2023-07-10 thomas * A
2087 795b88cb 2023-07-10 thomas *
2088 795b88cb 2023-07-10 thomas * 'got update' only handles linear cases:
2089 795b88cb 2023-07-10 thomas * Update forwards in time: A (base/yca) - B - C - D (commit)
2090 795b88cb 2023-07-10 thomas * Update backwards in time: D (base) - C - B - A (commit/yca)
2091 795b88cb 2023-07-10 thomas */
2092 795b88cb 2023-07-10 thomas if (allow_forwards_in_time_only) {
2093 795b88cb 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2094 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2095 795b88cb 2023-07-10 thomas } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2096 795b88cb 2023-07-10 thomas got_object_id_cmp(base_commit_id, yca_id) != 0)
2097 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2098 795b88cb 2023-07-10 thomas
2099 795b88cb 2023-07-10 thomas free(yca_id);
2100 795b88cb 2023-07-10 thomas return NULL;
2101 795b88cb 2023-07-10 thomas }
2102 795b88cb 2023-07-10 thomas
2103 795b88cb 2023-07-10 thomas static const struct got_error *
2104 795b88cb 2023-07-10 thomas check_same_branch(struct got_object_id *commit_id,
2105 795b88cb 2023-07-10 thomas struct got_reference *head_ref, struct got_repository *repo)
2106 795b88cb 2023-07-10 thomas {
2107 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2108 795b88cb 2023-07-10 thomas struct got_commit_graph *graph = NULL;
2109 795b88cb 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2110 795b88cb 2023-07-10 thomas
2111 795b88cb 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2112 795b88cb 2023-07-10 thomas if (err)
2113 795b88cb 2023-07-10 thomas goto done;
2114 795b88cb 2023-07-10 thomas
2115 795b88cb 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2116 795b88cb 2023-07-10 thomas goto done;
2117 795b88cb 2023-07-10 thomas
2118 795b88cb 2023-07-10 thomas err = got_commit_graph_open(&graph, "/", 1);
2119 795b88cb 2023-07-10 thomas if (err)
2120 795b88cb 2023-07-10 thomas goto done;
2121 795b88cb 2023-07-10 thomas
2122 0279329d 2024-03-30 thomas err = got_commit_graph_bfsort(graph, head_commit_id, repo,
2123 795b88cb 2023-07-10 thomas check_cancelled, NULL);
2124 795b88cb 2023-07-10 thomas if (err)
2125 795b88cb 2023-07-10 thomas goto done;
2126 795b88cb 2023-07-10 thomas
2127 795b88cb 2023-07-10 thomas for (;;) {
2128 795b88cb 2023-07-10 thomas struct got_object_id id;
2129 795b88cb 2023-07-10 thomas
2130 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
2131 795b88cb 2023-07-10 thomas check_cancelled, NULL);
2132 795b88cb 2023-07-10 thomas if (err) {
2133 795b88cb 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
2134 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_ANCESTRY);
2135 795b88cb 2023-07-10 thomas break;
2136 795b88cb 2023-07-10 thomas }
2137 795b88cb 2023-07-10 thomas
2138 795b88cb 2023-07-10 thomas if (got_object_id_cmp(&id, commit_id) == 0)
2139 795b88cb 2023-07-10 thomas break;
2140 795b88cb 2023-07-10 thomas }
2141 795b88cb 2023-07-10 thomas done:
2142 795b88cb 2023-07-10 thomas if (graph)
2143 795b88cb 2023-07-10 thomas got_commit_graph_close(graph);
2144 795b88cb 2023-07-10 thomas free(head_commit_id);
2145 795b88cb 2023-07-10 thomas return err;
2146 795b88cb 2023-07-10 thomas }
2147 795b88cb 2023-07-10 thomas
2148 795b88cb 2023-07-10 thomas static const struct got_error *
2149 795b88cb 2023-07-10 thomas checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2150 795b88cb 2023-07-10 thomas {
2151 795b88cb 2023-07-10 thomas static char msg[512];
2152 795b88cb 2023-07-10 thomas const char *branch_name;
2153 795b88cb 2023-07-10 thomas
2154 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(ref))
2155 795b88cb 2023-07-10 thomas branch_name = got_ref_get_symref_target(ref);
2156 795b88cb 2023-07-10 thomas else
2157 795b88cb 2023-07-10 thomas branch_name = got_ref_get_name(ref);
2158 795b88cb 2023-07-10 thomas
2159 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", branch_name, 11) == 0)
2160 795b88cb 2023-07-10 thomas branch_name += 11;
2161 795b88cb 2023-07-10 thomas
2162 795b88cb 2023-07-10 thomas snprintf(msg, sizeof(msg),
2163 795b88cb 2023-07-10 thomas "target commit is not contained in branch '%s'; "
2164 795b88cb 2023-07-10 thomas "the branch to use must be specified with -b; "
2165 795b88cb 2023-07-10 thomas "if necessary a new branch can be created for "
2166 795b88cb 2023-07-10 thomas "this commit with 'got branch -c %s BRANCH_NAME'",
2167 795b88cb 2023-07-10 thomas branch_name, commit_id_str);
2168 795b88cb 2023-07-10 thomas
2169 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_ANCESTRY, msg);
2170 795b88cb 2023-07-10 thomas }
2171 795b88cb 2023-07-10 thomas
2172 795b88cb 2023-07-10 thomas static const struct got_error *
2173 795b88cb 2023-07-10 thomas cmd_checkout(int argc, char *argv[])
2174 795b88cb 2023-07-10 thomas {
2175 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
2176 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
2177 795b88cb 2023-07-10 thomas struct got_reference *head_ref = NULL, *ref = NULL;
2178 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
2179 795b88cb 2023-07-10 thomas char *repo_path = NULL;
2180 795b88cb 2023-07-10 thomas char *worktree_path = NULL;
2181 795b88cb 2023-07-10 thomas const char *path_prefix = "";
2182 795b88cb 2023-07-10 thomas const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2183 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
2184 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2185 795b88cb 2023-07-10 thomas char *cwd = NULL;
2186 795b88cb 2023-07-10 thomas int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2187 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
2188 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg cpa;
2189 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2190 795b88cb 2023-07-10 thomas
2191 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
2192 795b88cb 2023-07-10 thomas
2193 795b88cb 2023-07-10 thomas #ifndef PROFILE
2194 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2195 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
2196 795b88cb 2023-07-10 thomas err(1, "pledge");
2197 795b88cb 2023-07-10 thomas #endif
2198 795b88cb 2023-07-10 thomas
2199 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2200 795b88cb 2023-07-10 thomas switch (ch) {
2201 795b88cb 2023-07-10 thomas case 'b':
2202 795b88cb 2023-07-10 thomas branch_name = optarg;
2203 795b88cb 2023-07-10 thomas break;
2204 795b88cb 2023-07-10 thomas case 'c':
2205 795b88cb 2023-07-10 thomas commit_id_str = strdup(optarg);
2206 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
2207 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2208 795b88cb 2023-07-10 thomas break;
2209 795b88cb 2023-07-10 thomas case 'E':
2210 795b88cb 2023-07-10 thomas allow_nonempty = 1;
2211 795b88cb 2023-07-10 thomas break;
2212 795b88cb 2023-07-10 thomas case 'p':
2213 795b88cb 2023-07-10 thomas path_prefix = optarg;
2214 795b88cb 2023-07-10 thomas break;
2215 795b88cb 2023-07-10 thomas case 'q':
2216 795b88cb 2023-07-10 thomas verbosity = -1;
2217 795b88cb 2023-07-10 thomas break;
2218 795b88cb 2023-07-10 thomas default:
2219 795b88cb 2023-07-10 thomas usage_checkout();
2220 795b88cb 2023-07-10 thomas /* NOTREACHED */
2221 795b88cb 2023-07-10 thomas }
2222 795b88cb 2023-07-10 thomas }
2223 795b88cb 2023-07-10 thomas
2224 795b88cb 2023-07-10 thomas argc -= optind;
2225 795b88cb 2023-07-10 thomas argv += optind;
2226 795b88cb 2023-07-10 thomas
2227 795b88cb 2023-07-10 thomas if (argc == 1) {
2228 795b88cb 2023-07-10 thomas char *base, *dotgit;
2229 795b88cb 2023-07-10 thomas const char *path;
2230 795b88cb 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
2231 795b88cb 2023-07-10 thomas if (repo_path == NULL)
2232 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath", argv[0]);
2233 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
2234 795b88cb 2023-07-10 thomas if (cwd == NULL) {
2235 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
2236 795b88cb 2023-07-10 thomas goto done;
2237 795b88cb 2023-07-10 thomas }
2238 795b88cb 2023-07-10 thomas if (path_prefix[0])
2239 795b88cb 2023-07-10 thomas path = path_prefix;
2240 795b88cb 2023-07-10 thomas else
2241 795b88cb 2023-07-10 thomas path = repo_path;
2242 795b88cb 2023-07-10 thomas error = got_path_basename(&base, path);
2243 795b88cb 2023-07-10 thomas if (error)
2244 795b88cb 2023-07-10 thomas goto done;
2245 795b88cb 2023-07-10 thomas dotgit = strstr(base, ".git");
2246 795b88cb 2023-07-10 thomas if (dotgit)
2247 795b88cb 2023-07-10 thomas *dotgit = '\0';
2248 795b88cb 2023-07-10 thomas if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2249 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
2250 795b88cb 2023-07-10 thomas free(base);
2251 795b88cb 2023-07-10 thomas goto done;
2252 795b88cb 2023-07-10 thomas }
2253 795b88cb 2023-07-10 thomas free(base);
2254 795b88cb 2023-07-10 thomas } else if (argc == 2) {
2255 795b88cb 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
2256 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
2257 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
2258 795b88cb 2023-07-10 thomas goto done;
2259 795b88cb 2023-07-10 thomas }
2260 795b88cb 2023-07-10 thomas worktree_path = realpath(argv[1], NULL);
2261 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2262 795b88cb 2023-07-10 thomas if (errno != ENOENT) {
2263 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
2264 795b88cb 2023-07-10 thomas argv[1]);
2265 795b88cb 2023-07-10 thomas goto done;
2266 795b88cb 2023-07-10 thomas }
2267 795b88cb 2023-07-10 thomas worktree_path = strdup(argv[1]);
2268 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2269 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
2270 795b88cb 2023-07-10 thomas goto done;
2271 795b88cb 2023-07-10 thomas }
2272 795b88cb 2023-07-10 thomas }
2273 795b88cb 2023-07-10 thomas } else
2274 795b88cb 2023-07-10 thomas usage_checkout();
2275 795b88cb 2023-07-10 thomas
2276 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
2277 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(worktree_path);
2278 795b88cb 2023-07-10 thomas
2279 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2280 795b88cb 2023-07-10 thomas if (error != NULL)
2281 795b88cb 2023-07-10 thomas goto done;
2282 795b88cb 2023-07-10 thomas
2283 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2284 795b88cb 2023-07-10 thomas if (error != NULL)
2285 795b88cb 2023-07-10 thomas goto done;
2286 795b88cb 2023-07-10 thomas
2287 795b88cb 2023-07-10 thomas /* Pre-create work tree path for unveil(2) */
2288 795b88cb 2023-07-10 thomas error = got_path_mkdir(worktree_path);
2289 795b88cb 2023-07-10 thomas if (error) {
2290 795b88cb 2023-07-10 thomas if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2291 795b88cb 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2292 795b88cb 2023-07-10 thomas goto done;
2293 795b88cb 2023-07-10 thomas if (!allow_nonempty &&
2294 795b88cb 2023-07-10 thomas !got_path_dir_is_empty(worktree_path)) {
2295 795b88cb 2023-07-10 thomas error = got_error_path(worktree_path,
2296 795b88cb 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
2297 795b88cb 2023-07-10 thomas goto done;
2298 795b88cb 2023-07-10 thomas }
2299 795b88cb 2023-07-10 thomas }
2300 795b88cb 2023-07-10 thomas
2301 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2302 795b88cb 2023-07-10 thomas if (error)
2303 795b88cb 2023-07-10 thomas goto done;
2304 795b88cb 2023-07-10 thomas
2305 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, branch_name, 0);
2306 795b88cb 2023-07-10 thomas if (error != NULL)
2307 795b88cb 2023-07-10 thomas goto done;
2308 795b88cb 2023-07-10 thomas
2309 ad10f64e 2023-07-19 thomas error = got_worktree_init(worktree_path, head_ref, path_prefix,
2310 ad10f64e 2023-07-19 thomas GOT_WORKTREE_CVG_DIR, repo);
2311 795b88cb 2023-07-10 thomas if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2312 795b88cb 2023-07-10 thomas goto done;
2313 795b88cb 2023-07-10 thomas
2314 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, worktree_path, GOT_WORKTREE_CVG_DIR);
2315 795b88cb 2023-07-10 thomas if (error != NULL)
2316 795b88cb 2023-07-10 thomas goto done;
2317 795b88cb 2023-07-10 thomas
2318 795b88cb 2023-07-10 thomas error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2319 795b88cb 2023-07-10 thomas path_prefix);
2320 795b88cb 2023-07-10 thomas if (error != NULL)
2321 795b88cb 2023-07-10 thomas goto done;
2322 795b88cb 2023-07-10 thomas if (!same_path_prefix) {
2323 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_PATH_PREFIX);
2324 795b88cb 2023-07-10 thomas goto done;
2325 795b88cb 2023-07-10 thomas }
2326 795b88cb 2023-07-10 thomas
2327 795b88cb 2023-07-10 thomas if (commit_id_str) {
2328 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
2329 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2330 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2331 795b88cb 2023-07-10 thomas NULL);
2332 795b88cb 2023-07-10 thomas if (error)
2333 795b88cb 2023-07-10 thomas goto done;
2334 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
2335 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2336 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
2337 795b88cb 2023-07-10 thomas if (error)
2338 795b88cb 2023-07-10 thomas goto done;
2339 795b88cb 2023-07-10 thomas error = check_linear_ancestry(commit_id,
2340 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
2341 795b88cb 2023-07-10 thomas if (error != NULL) {
2342 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
2343 795b88cb 2023-07-10 thomas error = checkout_ancestry_error(
2344 795b88cb 2023-07-10 thomas head_ref, commit_id_str);
2345 795b88cb 2023-07-10 thomas }
2346 795b88cb 2023-07-10 thomas goto done;
2347 795b88cb 2023-07-10 thomas }
2348 795b88cb 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
2349 795b88cb 2023-07-10 thomas if (error) {
2350 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
2351 795b88cb 2023-07-10 thomas error = checkout_ancestry_error(
2352 795b88cb 2023-07-10 thomas head_ref, commit_id_str);
2353 795b88cb 2023-07-10 thomas }
2354 795b88cb 2023-07-10 thomas goto done;
2355 795b88cb 2023-07-10 thomas }
2356 795b88cb 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
2357 795b88cb 2023-07-10 thomas commit_id);
2358 795b88cb 2023-07-10 thomas if (error)
2359 795b88cb 2023-07-10 thomas goto done;
2360 795b88cb 2023-07-10 thomas /* Expand potentially abbreviated commit ID string. */
2361 795b88cb 2023-07-10 thomas free(commit_id_str);
2362 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2363 795b88cb 2023-07-10 thomas if (error)
2364 795b88cb 2023-07-10 thomas goto done;
2365 795b88cb 2023-07-10 thomas } else {
2366 795b88cb 2023-07-10 thomas commit_id = got_object_id_dup(
2367 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
2368 795b88cb 2023-07-10 thomas if (commit_id == NULL) {
2369 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_object_id_dup");
2370 795b88cb 2023-07-10 thomas goto done;
2371 795b88cb 2023-07-10 thomas }
2372 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2373 795b88cb 2023-07-10 thomas if (error)
2374 795b88cb 2023-07-10 thomas goto done;
2375 795b88cb 2023-07-10 thomas }
2376 795b88cb 2023-07-10 thomas
2377 795b88cb 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
2378 795b88cb 2023-07-10 thomas if (error)
2379 795b88cb 2023-07-10 thomas goto done;
2380 795b88cb 2023-07-10 thomas cpa.worktree_path = worktree_path;
2381 795b88cb 2023-07-10 thomas cpa.had_base_commit_ref_error = 0;
2382 795b88cb 2023-07-10 thomas cpa.verbosity = verbosity;
2383 795b88cb 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
2384 795b88cb 2023-07-10 thomas checkout_progress, &cpa, check_cancelled, NULL);
2385 795b88cb 2023-07-10 thomas if (error != NULL)
2386 795b88cb 2023-07-10 thomas goto done;
2387 795b88cb 2023-07-10 thomas
2388 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(head_ref)) {
2389 795b88cb 2023-07-10 thomas error = got_ref_resolve_symbolic(&ref, repo, head_ref);
2390 795b88cb 2023-07-10 thomas if (error)
2391 795b88cb 2023-07-10 thomas goto done;
2392 795b88cb 2023-07-10 thomas refname = got_ref_get_name(ref);
2393 795b88cb 2023-07-10 thomas } else
2394 795b88cb 2023-07-10 thomas refname = got_ref_get_name(head_ref);
2395 795b88cb 2023-07-10 thomas printf("Checked out %s: %s\n", refname, commit_id_str);
2396 795b88cb 2023-07-10 thomas printf("Now shut up and hack\n");
2397 795b88cb 2023-07-10 thomas if (cpa.had_base_commit_ref_error)
2398 795b88cb 2023-07-10 thomas show_worktree_base_ref_warning();
2399 795b88cb 2023-07-10 thomas done:
2400 795b88cb 2023-07-10 thomas if (pack_fds) {
2401 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
2402 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2403 795b88cb 2023-07-10 thomas if (error == NULL)
2404 795b88cb 2023-07-10 thomas error = pack_err;
2405 795b88cb 2023-07-10 thomas }
2406 795b88cb 2023-07-10 thomas if (head_ref)
2407 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
2408 795b88cb 2023-07-10 thomas if (ref)
2409 795b88cb 2023-07-10 thomas got_ref_close(ref);
2410 795b88cb 2023-07-10 thomas if (repo) {
2411 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2412 795b88cb 2023-07-10 thomas if (error == NULL)
2413 795b88cb 2023-07-10 thomas error = close_err;
2414 795b88cb 2023-07-10 thomas }
2415 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2416 795b88cb 2023-07-10 thomas free(commit_id_str);
2417 795b88cb 2023-07-10 thomas free(commit_id);
2418 795b88cb 2023-07-10 thomas free(repo_path);
2419 795b88cb 2023-07-10 thomas free(worktree_path);
2420 795b88cb 2023-07-10 thomas free(cwd);
2421 795b88cb 2023-07-10 thomas return error;
2422 795b88cb 2023-07-10 thomas }
2423 795b88cb 2023-07-10 thomas
2424 795b88cb 2023-07-10 thomas struct got_update_progress_arg {
2425 795b88cb 2023-07-10 thomas int did_something;
2426 795b88cb 2023-07-10 thomas int conflicts;
2427 795b88cb 2023-07-10 thomas int obstructed;
2428 795b88cb 2023-07-10 thomas int not_updated;
2429 795b88cb 2023-07-10 thomas int missing;
2430 795b88cb 2023-07-10 thomas int not_deleted;
2431 795b88cb 2023-07-10 thomas int unversioned;
2432 795b88cb 2023-07-10 thomas int verbosity;
2433 795b88cb 2023-07-10 thomas };
2434 795b88cb 2023-07-10 thomas
2435 795b88cb 2023-07-10 thomas static void
2436 795b88cb 2023-07-10 thomas print_update_progress_stats(struct got_update_progress_arg *upa)
2437 795b88cb 2023-07-10 thomas {
2438 795b88cb 2023-07-10 thomas if (!upa->did_something)
2439 795b88cb 2023-07-10 thomas return;
2440 795b88cb 2023-07-10 thomas
2441 795b88cb 2023-07-10 thomas if (upa->conflicts > 0)
2442 795b88cb 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
2443 795b88cb 2023-07-10 thomas if (upa->obstructed > 0)
2444 795b88cb 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
2445 795b88cb 2023-07-10 thomas upa->obstructed);
2446 795b88cb 2023-07-10 thomas if (upa->not_updated > 0)
2447 795b88cb 2023-07-10 thomas printf("Files not updated because of existing merge "
2448 795b88cb 2023-07-10 thomas "conflicts: %d\n", upa->not_updated);
2449 795b88cb 2023-07-10 thomas }
2450 795b88cb 2023-07-10 thomas
2451 795b88cb 2023-07-10 thomas /*
2452 795b88cb 2023-07-10 thomas * The meaning of some status codes differs between merge-style operations and
2453 795b88cb 2023-07-10 thomas * update operations. For example, the ! status code means "file was missing"
2454 795b88cb 2023-07-10 thomas * if changes were merged into the work tree, and "missing file was restored"
2455 795b88cb 2023-07-10 thomas * if the work tree was updated. This function should be used by any operation
2456 795b88cb 2023-07-10 thomas * which merges changes into the work tree without updating the work tree.
2457 795b88cb 2023-07-10 thomas */
2458 795b88cb 2023-07-10 thomas static void
2459 795b88cb 2023-07-10 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
2460 795b88cb 2023-07-10 thomas {
2461 795b88cb 2023-07-10 thomas if (!upa->did_something)
2462 795b88cb 2023-07-10 thomas return;
2463 795b88cb 2023-07-10 thomas
2464 795b88cb 2023-07-10 thomas if (upa->conflicts > 0)
2465 795b88cb 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
2466 795b88cb 2023-07-10 thomas if (upa->obstructed > 0)
2467 795b88cb 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
2468 795b88cb 2023-07-10 thomas upa->obstructed);
2469 795b88cb 2023-07-10 thomas if (upa->missing > 0)
2470 795b88cb 2023-07-10 thomas printf("Files which had incoming changes but could not be "
2471 795b88cb 2023-07-10 thomas "found in the work tree: %d\n", upa->missing);
2472 795b88cb 2023-07-10 thomas if (upa->not_deleted > 0)
2473 795b88cb 2023-07-10 thomas printf("Files not deleted due to differences in deleted "
2474 795b88cb 2023-07-10 thomas "content: %d\n", upa->not_deleted);
2475 795b88cb 2023-07-10 thomas if (upa->unversioned > 0)
2476 795b88cb 2023-07-10 thomas printf("Files not merged because an unversioned file was "
2477 795b88cb 2023-07-10 thomas "found in the work tree: %d\n", upa->unversioned);
2478 795b88cb 2023-07-10 thomas }
2479 795b88cb 2023-07-10 thomas
2480 795b88cb 2023-07-10 thomas __dead static void
2481 795b88cb 2023-07-10 thomas usage_update(void)
2482 795b88cb 2023-07-10 thomas {
2483 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s update [-qtvX] [-c commit] [-r remote] "
2484 795b88cb 2023-07-10 thomas "[path ...]\n", getprogname());
2485 795b88cb 2023-07-10 thomas exit(1);
2486 795b88cb 2023-07-10 thomas }
2487 795b88cb 2023-07-10 thomas
2488 795b88cb 2023-07-10 thomas static const struct got_error *
2489 795b88cb 2023-07-10 thomas update_progress(void *arg, unsigned char status, const char *path)
2490 795b88cb 2023-07-10 thomas {
2491 795b88cb 2023-07-10 thomas struct got_update_progress_arg *upa = arg;
2492 795b88cb 2023-07-10 thomas
2493 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_EXISTS ||
2494 795b88cb 2023-07-10 thomas status == GOT_STATUS_BASE_REF_ERR)
2495 795b88cb 2023-07-10 thomas return NULL;
2496 795b88cb 2023-07-10 thomas
2497 795b88cb 2023-07-10 thomas upa->did_something = 1;
2498 795b88cb 2023-07-10 thomas
2499 795b88cb 2023-07-10 thomas /* Base commit bump happens silently. */
2500 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2501 795b88cb 2023-07-10 thomas return NULL;
2502 795b88cb 2023-07-10 thomas
2503 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT)
2504 795b88cb 2023-07-10 thomas upa->conflicts++;
2505 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_OBSTRUCTED)
2506 795b88cb 2023-07-10 thomas upa->obstructed++;
2507 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
2508 795b88cb 2023-07-10 thomas upa->not_updated++;
2509 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_MISSING)
2510 795b88cb 2023-07-10 thomas upa->missing++;
2511 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_DELETE)
2512 795b88cb 2023-07-10 thomas upa->not_deleted++;
2513 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
2514 795b88cb 2023-07-10 thomas upa->unversioned++;
2515 795b88cb 2023-07-10 thomas
2516 795b88cb 2023-07-10 thomas while (path[0] == '/')
2517 795b88cb 2023-07-10 thomas path++;
2518 795b88cb 2023-07-10 thomas if (upa->verbosity >= 0)
2519 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
2520 795b88cb 2023-07-10 thomas
2521 795b88cb 2023-07-10 thomas return NULL;
2522 795b88cb 2023-07-10 thomas }
2523 795b88cb 2023-07-10 thomas
2524 795b88cb 2023-07-10 thomas static const struct got_error *
2525 795b88cb 2023-07-10 thomas check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2526 795b88cb 2023-07-10 thomas {
2527 795b88cb 2023-07-10 thomas const struct got_error *err;
2528 795b88cb 2023-07-10 thomas int in_progress;
2529 795b88cb 2023-07-10 thomas
2530 795b88cb 2023-07-10 thomas err = got_worktree_rebase_in_progress(&in_progress, worktree);
2531 795b88cb 2023-07-10 thomas if (err)
2532 795b88cb 2023-07-10 thomas return err;
2533 795b88cb 2023-07-10 thomas if (in_progress)
2534 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_REBASING);
2535 795b88cb 2023-07-10 thomas
2536 795b88cb 2023-07-10 thomas err = got_worktree_histedit_in_progress(&in_progress, worktree);
2537 795b88cb 2023-07-10 thomas if (err)
2538 795b88cb 2023-07-10 thomas return err;
2539 795b88cb 2023-07-10 thomas if (in_progress)
2540 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_HISTEDIT_BUSY);
2541 795b88cb 2023-07-10 thomas
2542 795b88cb 2023-07-10 thomas return NULL;
2543 795b88cb 2023-07-10 thomas }
2544 795b88cb 2023-07-10 thomas
2545 795b88cb 2023-07-10 thomas static const struct got_error *
2546 795b88cb 2023-07-10 thomas check_merge_in_progress(struct got_worktree *worktree,
2547 795b88cb 2023-07-10 thomas struct got_repository *repo)
2548 795b88cb 2023-07-10 thomas {
2549 795b88cb 2023-07-10 thomas const struct got_error *err;
2550 795b88cb 2023-07-10 thomas int in_progress;
2551 795b88cb 2023-07-10 thomas
2552 795b88cb 2023-07-10 thomas err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
2553 795b88cb 2023-07-10 thomas if (err)
2554 795b88cb 2023-07-10 thomas return err;
2555 795b88cb 2023-07-10 thomas if (in_progress)
2556 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_MERGE_BUSY);
2557 795b88cb 2023-07-10 thomas
2558 795b88cb 2023-07-10 thomas return NULL;
2559 795b88cb 2023-07-10 thomas }
2560 795b88cb 2023-07-10 thomas
2561 795b88cb 2023-07-10 thomas static const struct got_error *
2562 795b88cb 2023-07-10 thomas get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2563 795b88cb 2023-07-10 thomas char *argv[], struct got_worktree *worktree)
2564 795b88cb 2023-07-10 thomas {
2565 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2566 795b88cb 2023-07-10 thomas char *path;
2567 795b88cb 2023-07-10 thomas struct got_pathlist_entry *new;
2568 795b88cb 2023-07-10 thomas int i;
2569 795b88cb 2023-07-10 thomas
2570 795b88cb 2023-07-10 thomas if (argc == 0) {
2571 795b88cb 2023-07-10 thomas path = strdup("");
2572 795b88cb 2023-07-10 thomas if (path == NULL)
2573 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2574 795b88cb 2023-07-10 thomas return got_pathlist_append(paths, path, NULL);
2575 795b88cb 2023-07-10 thomas }
2576 795b88cb 2023-07-10 thomas
2577 795b88cb 2023-07-10 thomas for (i = 0; i < argc; i++) {
2578 795b88cb 2023-07-10 thomas err = got_worktree_resolve_path(&path, worktree, argv[i]);
2579 795b88cb 2023-07-10 thomas if (err)
2580 795b88cb 2023-07-10 thomas break;
2581 795b88cb 2023-07-10 thomas err = got_pathlist_insert(&new, paths, path, NULL);
2582 795b88cb 2023-07-10 thomas if (err || new == NULL /* duplicate */) {
2583 795b88cb 2023-07-10 thomas free(path);
2584 795b88cb 2023-07-10 thomas if (err)
2585 795b88cb 2023-07-10 thomas break;
2586 795b88cb 2023-07-10 thomas }
2587 795b88cb 2023-07-10 thomas }
2588 795b88cb 2023-07-10 thomas
2589 795b88cb 2023-07-10 thomas return err;
2590 795b88cb 2023-07-10 thomas }
2591 795b88cb 2023-07-10 thomas
2592 795b88cb 2023-07-10 thomas static const struct got_error *
2593 795b88cb 2023-07-10 thomas wrap_not_worktree_error(const struct got_error *orig_err,
2594 795b88cb 2023-07-10 thomas const char *cmdname, const char *path)
2595 795b88cb 2023-07-10 thomas {
2596 795b88cb 2023-07-10 thomas const struct got_error *err;
2597 795b88cb 2023-07-10 thomas struct got_repository *repo;
2598 795b88cb 2023-07-10 thomas static char msg[512];
2599 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2600 795b88cb 2023-07-10 thomas
2601 795b88cb 2023-07-10 thomas err = got_repo_pack_fds_open(&pack_fds);
2602 795b88cb 2023-07-10 thomas if (err)
2603 795b88cb 2023-07-10 thomas return err;
2604 795b88cb 2023-07-10 thomas
2605 795b88cb 2023-07-10 thomas err = got_repo_open(&repo, path, NULL, pack_fds);
2606 795b88cb 2023-07-10 thomas if (err)
2607 795b88cb 2023-07-10 thomas return orig_err;
2608 795b88cb 2023-07-10 thomas
2609 795b88cb 2023-07-10 thomas snprintf(msg, sizeof(msg),
2610 795b88cb 2023-07-10 thomas "'got %s' needs a work tree in addition to a git repository\n"
2611 795b88cb 2023-07-10 thomas "Work trees can be checked out from this Git repository with "
2612 795b88cb 2023-07-10 thomas "'got checkout'.\n"
2613 795b88cb 2023-07-10 thomas "The got(1) manual page contains more information.", cmdname);
2614 795b88cb 2023-07-10 thomas err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2615 795b88cb 2023-07-10 thomas if (repo) {
2616 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2617 795b88cb 2023-07-10 thomas if (err == NULL)
2618 795b88cb 2023-07-10 thomas err = close_err;
2619 795b88cb 2023-07-10 thomas }
2620 795b88cb 2023-07-10 thomas if (pack_fds) {
2621 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
2622 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2623 795b88cb 2023-07-10 thomas if (err == NULL)
2624 795b88cb 2023-07-10 thomas err = pack_err;
2625 795b88cb 2023-07-10 thomas }
2626 795b88cb 2023-07-10 thomas return err;
2627 795b88cb 2023-07-10 thomas }
2628 795b88cb 2023-07-10 thomas
2629 795b88cb 2023-07-10 thomas static const struct got_error *
2630 795b88cb 2023-07-10 thomas cmd_update(int argc, char *argv[])
2631 795b88cb 2023-07-10 thomas {
2632 795b88cb 2023-07-10 thomas const struct got_error *error = NULL, *unlock_err;
2633 795b88cb 2023-07-10 thomas char *worktree_path = NULL;
2634 795b88cb 2023-07-10 thomas const char *repo_path = NULL;
2635 795b88cb 2023-07-10 thomas const char *remote_name = NULL;
2636 795b88cb 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
2637 795b88cb 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
2638 795b88cb 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
2639 795b88cb 2023-07-10 thomas int nremotes;
2640 795b88cb 2023-07-10 thomas char *id_str = NULL;
2641 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
2642 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
2643 795b88cb 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2644 795b88cb 2023-07-10 thomas struct got_pathlist_head paths, refs, symrefs;
2645 795b88cb 2023-07-10 thomas struct got_pathlist_head wanted_branches, wanted_refs;
2646 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
2647 795b88cb 2023-07-10 thomas struct got_reflist_head remote_refs;
2648 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
2649 795b88cb 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
2650 795b88cb 2023-07-10 thomas int i, ch, fetchfd = -1, fetchstatus;
2651 795b88cb 2023-07-10 thomas pid_t fetchpid = -1;
2652 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2653 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
2654 795b88cb 2023-07-10 thomas int verbosity = 0;
2655 795b88cb 2023-07-10 thomas int delete_remote = 0;
2656 795b88cb 2023-07-10 thomas int replace_tags = 0;
2657 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2658 795b88cb 2023-07-10 thomas const char *remote_head = NULL, *worktree_branch = NULL;
2659 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2660 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
2661 795b88cb 2023-07-10 thomas const char *refname;
2662 795b88cb 2023-07-10 thomas struct got_reference *head_ref = NULL;
2663 795b88cb 2023-07-10 thomas
2664 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
2665 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2666 795b88cb 2023-07-10 thomas TAILQ_INIT(&symrefs);
2667 795b88cb 2023-07-10 thomas TAILQ_INIT(&remote_refs);
2668 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2669 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2670 795b88cb 2023-07-10 thomas
2671 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:qr:vX")) != -1) {
2672 795b88cb 2023-07-10 thomas switch (ch) {
2673 795b88cb 2023-07-10 thomas case 'c':
2674 795b88cb 2023-07-10 thomas commit_id_str = strdup(optarg);
2675 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
2676 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2677 795b88cb 2023-07-10 thomas break;
2678 795b88cb 2023-07-10 thomas case 't':
2679 795b88cb 2023-07-10 thomas replace_tags = 1;
2680 795b88cb 2023-07-10 thomas break;
2681 795b88cb 2023-07-10 thomas case 'q':
2682 795b88cb 2023-07-10 thomas verbosity = -1;
2683 795b88cb 2023-07-10 thomas break;
2684 795b88cb 2023-07-10 thomas case 'r':
2685 795b88cb 2023-07-10 thomas remote_name = optarg;
2686 795b88cb 2023-07-10 thomas break;
2687 795b88cb 2023-07-10 thomas case 'v':
2688 795b88cb 2023-07-10 thomas if (verbosity < 0)
2689 795b88cb 2023-07-10 thomas verbosity = 0;
2690 795b88cb 2023-07-10 thomas else if (verbosity < 3)
2691 795b88cb 2023-07-10 thomas verbosity++;
2692 795b88cb 2023-07-10 thomas break;
2693 795b88cb 2023-07-10 thomas case 'X':
2694 795b88cb 2023-07-10 thomas delete_remote = 1;
2695 795b88cb 2023-07-10 thomas break;
2696 795b88cb 2023-07-10 thomas default:
2697 795b88cb 2023-07-10 thomas usage_update();
2698 795b88cb 2023-07-10 thomas break;
2699 795b88cb 2023-07-10 thomas }
2700 795b88cb 2023-07-10 thomas }
2701 795b88cb 2023-07-10 thomas argc -= optind;
2702 795b88cb 2023-07-10 thomas argv += optind;
2703 795b88cb 2023-07-10 thomas
2704 795b88cb 2023-07-10 thomas if (delete_remote) {
2705 795b88cb 2023-07-10 thomas if (replace_tags)
2706 795b88cb 2023-07-10 thomas option_conflict('X', 't');
2707 795b88cb 2023-07-10 thomas if (remote_name == NULL)
2708 795b88cb 2023-07-10 thomas errx(1, "-X option requires a remote name");
2709 795b88cb 2023-07-10 thomas }
2710 795b88cb 2023-07-10 thomas if (remote_name == NULL)
2711 795b88cb 2023-07-10 thomas remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2712 795b88cb 2023-07-10 thomas
2713 795b88cb 2023-07-10 thomas worktree_path = getcwd(NULL, 0);
2714 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2715 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
2716 795b88cb 2023-07-10 thomas goto done;
2717 795b88cb 2023-07-10 thomas }
2718 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, worktree_path, GOT_WORKTREE_CVG_DIR);
2719 795b88cb 2023-07-10 thomas if (error) {
2720 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
2721 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "update",
2722 795b88cb 2023-07-10 thomas worktree_path);
2723 795b88cb 2023-07-10 thomas goto done;
2724 795b88cb 2023-07-10 thomas }
2725 795b88cb 2023-07-10 thomas repo_path = got_worktree_get_repo_path(worktree);
2726 795b88cb 2023-07-10 thomas
2727 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2728 795b88cb 2023-07-10 thomas if (error != NULL)
2729 795b88cb 2023-07-10 thomas goto done;
2730 795b88cb 2023-07-10 thomas
2731 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2732 795b88cb 2023-07-10 thomas if (error)
2733 795b88cb 2023-07-10 thomas goto done;
2734 795b88cb 2023-07-10 thomas
2735 795b88cb 2023-07-10 thomas error = check_rebase_or_histedit_in_progress(worktree);
2736 795b88cb 2023-07-10 thomas if (error)
2737 795b88cb 2023-07-10 thomas goto done;
2738 795b88cb 2023-07-10 thomas error = check_merge_in_progress(worktree, repo);
2739 795b88cb 2023-07-10 thomas if (error)
2740 795b88cb 2023-07-10 thomas goto done;
2741 795b88cb 2023-07-10 thomas
2742 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2743 795b88cb 2023-07-10 thomas if (error)
2744 795b88cb 2023-07-10 thomas goto done;
2745 795b88cb 2023-07-10 thomas
2746 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
2747 795b88cb 2023-07-10 thomas if (worktree_conf) {
2748 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2749 795b88cb 2023-07-10 thomas worktree_conf);
2750 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2751 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2752 795b88cb 2023-07-10 thomas remote = &remotes[i];
2753 795b88cb 2023-07-10 thomas break;
2754 795b88cb 2023-07-10 thomas }
2755 795b88cb 2023-07-10 thomas }
2756 795b88cb 2023-07-10 thomas }
2757 795b88cb 2023-07-10 thomas
2758 795b88cb 2023-07-10 thomas if (remote == NULL) {
2759 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
2760 795b88cb 2023-07-10 thomas if (repo_conf) {
2761 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2762 795b88cb 2023-07-10 thomas repo_conf);
2763 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2764 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2765 795b88cb 2023-07-10 thomas remote = &remotes[i];
2766 795b88cb 2023-07-10 thomas break;
2767 795b88cb 2023-07-10 thomas }
2768 795b88cb 2023-07-10 thomas }
2769 795b88cb 2023-07-10 thomas }
2770 795b88cb 2023-07-10 thomas }
2771 795b88cb 2023-07-10 thomas if (remote == NULL) {
2772 795b88cb 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2773 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2774 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2775 795b88cb 2023-07-10 thomas remote = &remotes[i];
2776 795b88cb 2023-07-10 thomas break;
2777 795b88cb 2023-07-10 thomas }
2778 795b88cb 2023-07-10 thomas }
2779 795b88cb 2023-07-10 thomas }
2780 795b88cb 2023-07-10 thomas if (remote == NULL) {
2781 795b88cb 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2782 795b88cb 2023-07-10 thomas goto done;
2783 795b88cb 2023-07-10 thomas }
2784 795b88cb 2023-07-10 thomas
2785 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2786 795b88cb 2023-07-10 thomas &repo_name, remote->fetch_url);
2787 795b88cb 2023-07-10 thomas if (error)
2788 795b88cb 2023-07-10 thomas goto done;
2789 795b88cb 2023-07-10 thomas
2790 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
2791 795b88cb 2023-07-10 thomas #ifndef PROFILE
2792 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2793 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
2794 795b88cb 2023-07-10 thomas err(1, "pledge");
2795 795b88cb 2023-07-10 thomas #endif
2796 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
2797 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
2798 795b88cb 2023-07-10 thomas #ifndef PROFILE
2799 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2800 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
2801 795b88cb 2023-07-10 thomas err(1, "pledge");
2802 795b88cb 2023-07-10 thomas #endif
2803 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
2804 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
2805 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2806 795b88cb 2023-07-10 thomas goto done;
2807 795b88cb 2023-07-10 thomas } else {
2808 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2809 795b88cb 2023-07-10 thomas goto done;
2810 795b88cb 2023-07-10 thomas }
2811 795b88cb 2023-07-10 thomas
2812 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
2813 795b88cb 2023-07-10 thomas if (error)
2814 795b88cb 2023-07-10 thomas goto done;
2815 795b88cb 2023-07-10 thomas
2816 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
2817 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
2818 795b88cb 2023-07-10 thomas if (error)
2819 795b88cb 2023-07-10 thomas goto done;
2820 795b88cb 2023-07-10 thomas
2821 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
2822 795b88cb 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2823 795b88cb 2023-07-10 thomas remote->name, proto, host,
2824 795b88cb 2023-07-10 thomas port ? ":" : "", port ? port : "",
2825 795b88cb 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
2826 795b88cb 2023-07-10 thomas }
2827 795b88cb 2023-07-10 thomas
2828 795b88cb 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2829 795b88cb 2023-07-10 thomas server_path, verbosity);
2830 795b88cb 2023-07-10 thomas if (error)
2831 795b88cb 2023-07-10 thomas goto done;
2832 795b88cb 2023-07-10 thomas
2833 795b88cb 2023-07-10 thomas /*
2834 795b88cb 2023-07-10 thomas * If set, get this remote's HEAD ref target so
2835 795b88cb 2023-07-10 thomas * if it has changed on the server we can fetch it.
2836 795b88cb 2023-07-10 thomas */
2837 795b88cb 2023-07-10 thomas error = got_ref_list(&remote_refs, repo, "refs/remotes",
2838 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
2839 795b88cb 2023-07-10 thomas if (error)
2840 795b88cb 2023-07-10 thomas goto done;
2841 795b88cb 2023-07-10 thomas
2842 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &remote_refs, entry) {
2843 795b88cb 2023-07-10 thomas const char *remote_refname, *remote_target;
2844 795b88cb 2023-07-10 thomas size_t remote_name_len;
2845 795b88cb 2023-07-10 thomas
2846 795b88cb 2023-07-10 thomas if (!got_ref_is_symbolic(re->ref))
2847 795b88cb 2023-07-10 thomas continue;
2848 795b88cb 2023-07-10 thomas
2849 795b88cb 2023-07-10 thomas remote_name_len = strlen(remote->name);
2850 795b88cb 2023-07-10 thomas remote_refname = got_ref_get_name(re->ref);
2851 795b88cb 2023-07-10 thomas
2852 795b88cb 2023-07-10 thomas /* we only want refs/remotes/$remote->name/HEAD */
2853 795b88cb 2023-07-10 thomas if (strncmp(remote_refname + 13, remote->name,
2854 795b88cb 2023-07-10 thomas remote_name_len) != 0)
2855 795b88cb 2023-07-10 thomas continue;
2856 795b88cb 2023-07-10 thomas
2857 795b88cb 2023-07-10 thomas if (strcmp(remote_refname + remote_name_len + 14,
2858 795b88cb 2023-07-10 thomas GOT_REF_HEAD) != 0)
2859 795b88cb 2023-07-10 thomas continue;
2860 795b88cb 2023-07-10 thomas
2861 795b88cb 2023-07-10 thomas /*
2862 795b88cb 2023-07-10 thomas * Take the name itself because we already
2863 795b88cb 2023-07-10 thomas * only match with refs/heads/ in fetch_pack().
2864 795b88cb 2023-07-10 thomas */
2865 795b88cb 2023-07-10 thomas remote_target = got_ref_get_symref_target(re->ref);
2866 795b88cb 2023-07-10 thomas remote_head = remote_target + remote_name_len + 14;
2867 795b88cb 2023-07-10 thomas break;
2868 795b88cb 2023-07-10 thomas }
2869 795b88cb 2023-07-10 thomas
2870 795b88cb 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
2871 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
2872 795b88cb 2023-07-10 thomas worktree_branch = refname;
2873 795b88cb 2023-07-10 thomas
2874 795b88cb 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2875 795b88cb 2023-07-10 thomas fpa.last_p_indexed = -1;
2876 795b88cb 2023-07-10 thomas fpa.last_p_resolved = -1;
2877 795b88cb 2023-07-10 thomas fpa.verbosity = verbosity;
2878 795b88cb 2023-07-10 thomas fpa.repo = repo;
2879 795b88cb 2023-07-10 thomas fpa.create_configs = 0;
2880 795b88cb 2023-07-10 thomas fpa.configs_created = 0;
2881 795b88cb 2023-07-10 thomas memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2882 795b88cb 2023-07-10 thomas
2883 795b88cb 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2884 795b88cb 2023-07-10 thomas remote->mirror_references, 0, &wanted_branches, &wanted_refs,
2885 795b88cb 2023-07-10 thomas 0, verbosity, fetchfd, repo, worktree_branch, remote_head,
2886 795b88cb 2023-07-10 thomas 0, fetch_progress, &fpa);
2887 795b88cb 2023-07-10 thomas if (error)
2888 795b88cb 2023-07-10 thomas goto done;
2889 795b88cb 2023-07-10 thomas
2890 795b88cb 2023-07-10 thomas if (pack_hash != NULL && verbosity >= 0) {
2891 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
2892 795b88cb 2023-07-10 thomas if (error)
2893 795b88cb 2023-07-10 thomas goto done;
2894 795b88cb 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
2895 795b88cb 2023-07-10 thomas free(id_str);
2896 795b88cb 2023-07-10 thomas id_str = NULL;
2897 795b88cb 2023-07-10 thomas }
2898 795b88cb 2023-07-10 thomas
2899 795b88cb 2023-07-10 thomas /* Update references provided with the pack file. */
2900 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
2901 795b88cb 2023-07-10 thomas const char *refname = pe->path;
2902 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
2903 795b88cb 2023-07-10 thomas struct got_reference *ref;
2904 795b88cb 2023-07-10 thomas
2905 795b88cb 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname)) {
2906 795b88cb 2023-07-10 thomas error = update_wanted_ref(refname, id,
2907 795b88cb 2023-07-10 thomas remote->name, verbosity, repo);
2908 795b88cb 2023-07-10 thomas if (error)
2909 795b88cb 2023-07-10 thomas goto done;
2910 795b88cb 2023-07-10 thomas continue;
2911 795b88cb 2023-07-10 thomas }
2912 795b88cb 2023-07-10 thomas
2913 795b88cb 2023-07-10 thomas error = got_ref_open(&ref, repo, refname, 1);
2914 795b88cb 2023-07-10 thomas if (error) {
2915 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
2916 795b88cb 2023-07-10 thomas goto done;
2917 795b88cb 2023-07-10 thomas error = create_ref(refname, id, verbosity,
2918 795b88cb 2023-07-10 thomas repo);
2919 795b88cb 2023-07-10 thomas if (error)
2920 795b88cb 2023-07-10 thomas goto done;
2921 795b88cb 2023-07-10 thomas } else {
2922 795b88cb 2023-07-10 thomas error = update_ref(ref, id, replace_tags,
2923 795b88cb 2023-07-10 thomas verbosity-1, repo);
2924 795b88cb 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2925 795b88cb 2023-07-10 thomas if (unlock_err && error == NULL)
2926 795b88cb 2023-07-10 thomas error = unlock_err;
2927 795b88cb 2023-07-10 thomas got_ref_close(ref);
2928 795b88cb 2023-07-10 thomas if (error)
2929 795b88cb 2023-07-10 thomas goto done;
2930 795b88cb 2023-07-10 thomas }
2931 795b88cb 2023-07-10 thomas }
2932 795b88cb 2023-07-10 thomas
2933 795b88cb 2023-07-10 thomas /* Update worktree */
2934 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
2935 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
2936 795b88cb 2023-07-10 thomas if (error != NULL)
2937 795b88cb 2023-07-10 thomas goto done;
2938 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
2939 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
2940 795b88cb 2023-07-10 thomas if (error != NULL)
2941 795b88cb 2023-07-10 thomas goto done;
2942 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2943 795b88cb 2023-07-10 thomas if (error != NULL)
2944 795b88cb 2023-07-10 thomas goto done;
2945 795b88cb 2023-07-10 thomas } else {
2946 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
2947 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2948 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2949 795b88cb 2023-07-10 thomas NULL);
2950 795b88cb 2023-07-10 thomas if (error)
2951 795b88cb 2023-07-10 thomas goto done;
2952 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
2953 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2954 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
2955 795b88cb 2023-07-10 thomas free(commit_id_str);
2956 795b88cb 2023-07-10 thomas commit_id_str = NULL;
2957 795b88cb 2023-07-10 thomas if (error)
2958 795b88cb 2023-07-10 thomas goto done;
2959 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2960 795b88cb 2023-07-10 thomas if (error)
2961 795b88cb 2023-07-10 thomas goto done;
2962 795b88cb 2023-07-10 thomas }
2963 795b88cb 2023-07-10 thomas
2964 795b88cb 2023-07-10 thomas
2965 795b88cb 2023-07-10 thomas error = check_linear_ancestry(commit_id,
2966 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
2967 795b88cb 2023-07-10 thomas if (error != NULL) {
2968 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY)
2969 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_BRANCH_MOVED);
2970 795b88cb 2023-07-10 thomas goto done;
2971 795b88cb 2023-07-10 thomas }
2972 795b88cb 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
2973 795b88cb 2023-07-10 thomas if (error)
2974 795b88cb 2023-07-10 thomas goto done;
2975 795b88cb 2023-07-10 thomas
2976 795b88cb 2023-07-10 thomas if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2977 795b88cb 2023-07-10 thomas commit_id) != 0) {
2978 795b88cb 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
2979 795b88cb 2023-07-10 thomas commit_id);
2980 795b88cb 2023-07-10 thomas if (error)
2981 795b88cb 2023-07-10 thomas goto done;
2982 795b88cb 2023-07-10 thomas }
2983 795b88cb 2023-07-10 thomas
2984 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
2985 795b88cb 2023-07-10 thomas upa.verbosity = verbosity;
2986 795b88cb 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
2987 795b88cb 2023-07-10 thomas update_progress, &upa, check_cancelled, NULL);
2988 795b88cb 2023-07-10 thomas if (error != NULL)
2989 795b88cb 2023-07-10 thomas goto done;
2990 795b88cb 2023-07-10 thomas
2991 795b88cb 2023-07-10 thomas if (upa.did_something) {
2992 795b88cb 2023-07-10 thomas printf("Updated to %s: %s\n",
2993 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), commit_id_str);
2994 795b88cb 2023-07-10 thomas } else
2995 795b88cb 2023-07-10 thomas printf("Already up-to-date\n");
2996 795b88cb 2023-07-10 thomas
2997 795b88cb 2023-07-10 thomas print_update_progress_stats(&upa);
2998 795b88cb 2023-07-10 thomas done:
2999 795b88cb 2023-07-10 thomas if (fetchpid > 0) {
3000 795b88cb 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
3001 795b88cb 2023-07-10 thomas error = got_error_from_errno("kill");
3002 795b88cb 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
3003 795b88cb 2023-07-10 thomas error = got_error_from_errno("waitpid");
3004 795b88cb 2023-07-10 thomas }
3005 795b88cb 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
3006 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
3007 795b88cb 2023-07-10 thomas if (repo) {
3008 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
3009 795b88cb 2023-07-10 thomas if (error == NULL)
3010 795b88cb 2023-07-10 thomas error = close_err;
3011 795b88cb 2023-07-10 thomas }
3012 795b88cb 2023-07-10 thomas if (worktree)
3013 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
3014 795b88cb 2023-07-10 thomas if (pack_fds) {
3015 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
3016 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
3017 795b88cb 2023-07-10 thomas if (error == NULL)
3018 795b88cb 2023-07-10 thomas error = pack_err;
3019 795b88cb 2023-07-10 thomas }
3020 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3021 795b88cb 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
3022 795b88cb 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
3023 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
3024 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
3025 795b88cb 2023-07-10 thomas got_ref_list_free(&remote_refs);
3026 795b88cb 2023-07-10 thomas free(id_str);
3027 795b88cb 2023-07-10 thomas free(worktree_path);
3028 795b88cb 2023-07-10 thomas free(pack_hash);
3029 795b88cb 2023-07-10 thomas free(proto);
3030 795b88cb 2023-07-10 thomas free(host);
3031 795b88cb 2023-07-10 thomas free(port);
3032 795b88cb 2023-07-10 thomas free(server_path);
3033 795b88cb 2023-07-10 thomas free(repo_name);
3034 795b88cb 2023-07-10 thomas free(commit_id);
3035 795b88cb 2023-07-10 thomas free(commit_id_str);
3036 795b88cb 2023-07-10 thomas return error;
3037 795b88cb 2023-07-10 thomas }
3038 795b88cb 2023-07-10 thomas
3039 795b88cb 2023-07-10 thomas static const struct got_error *
3040 795b88cb 2023-07-10 thomas diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3041 795b88cb 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3042 795b88cb 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3043 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3044 795b88cb 2023-07-10 thomas {
3045 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3046 795b88cb 2023-07-10 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3047 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3048 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3049 795b88cb 2023-07-10 thomas
3050 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3051 795b88cb 2023-07-10 thomas if (fd1 == -1)
3052 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
3053 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3054 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3055 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3056 795b88cb 2023-07-10 thomas goto done;
3057 795b88cb 2023-07-10 thomas }
3058 795b88cb 2023-07-10 thomas
3059 795b88cb 2023-07-10 thomas if (blob_id1) {
3060 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3061 795b88cb 2023-07-10 thomas fd1);
3062 795b88cb 2023-07-10 thomas if (err)
3063 795b88cb 2023-07-10 thomas goto done;
3064 795b88cb 2023-07-10 thomas }
3065 795b88cb 2023-07-10 thomas
3066 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3067 795b88cb 2023-07-10 thomas if (err)
3068 795b88cb 2023-07-10 thomas goto done;
3069 795b88cb 2023-07-10 thomas
3070 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3071 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3072 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3073 795b88cb 2023-07-10 thomas goto done;
3074 795b88cb 2023-07-10 thomas }
3075 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3076 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3077 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3078 795b88cb 2023-07-10 thomas goto done;
3079 795b88cb 2023-07-10 thomas }
3080 795b88cb 2023-07-10 thomas
3081 795b88cb 2023-07-10 thomas while (path[0] == '/')
3082 795b88cb 2023-07-10 thomas path++;
3083 795b88cb 2023-07-10 thomas err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3084 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3085 795b88cb 2023-07-10 thomas force_text_diff, dsa, outfile);
3086 795b88cb 2023-07-10 thomas done:
3087 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3088 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3089 795b88cb 2023-07-10 thomas if (blob1)
3090 795b88cb 2023-07-10 thomas got_object_blob_close(blob1);
3091 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3092 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3093 795b88cb 2023-07-10 thomas if (blob2)
3094 795b88cb 2023-07-10 thomas got_object_blob_close(blob2);
3095 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3096 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3097 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3098 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3099 795b88cb 2023-07-10 thomas return err;
3100 795b88cb 2023-07-10 thomas }
3101 795b88cb 2023-07-10 thomas
3102 795b88cb 2023-07-10 thomas static const struct got_error *
3103 795b88cb 2023-07-10 thomas diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3104 795b88cb 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3105 795b88cb 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3106 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3107 795b88cb 2023-07-10 thomas {
3108 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3109 795b88cb 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3110 795b88cb 2023-07-10 thomas struct got_diff_blob_output_unidiff_arg arg;
3111 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3112 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3113 795b88cb 2023-07-10 thomas
3114 795b88cb 2023-07-10 thomas if (tree_id1) {
3115 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3116 795b88cb 2023-07-10 thomas if (err)
3117 795b88cb 2023-07-10 thomas goto done;
3118 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3119 795b88cb 2023-07-10 thomas if (fd1 == -1) {
3120 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3121 795b88cb 2023-07-10 thomas goto done;
3122 795b88cb 2023-07-10 thomas }
3123 795b88cb 2023-07-10 thomas }
3124 795b88cb 2023-07-10 thomas
3125 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3126 795b88cb 2023-07-10 thomas if (err)
3127 795b88cb 2023-07-10 thomas goto done;
3128 795b88cb 2023-07-10 thomas
3129 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3130 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3131 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3132 795b88cb 2023-07-10 thomas goto done;
3133 795b88cb 2023-07-10 thomas }
3134 795b88cb 2023-07-10 thomas
3135 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3136 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3137 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3138 795b88cb 2023-07-10 thomas goto done;
3139 795b88cb 2023-07-10 thomas }
3140 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3141 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3142 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3143 795b88cb 2023-07-10 thomas goto done;
3144 795b88cb 2023-07-10 thomas }
3145 795b88cb 2023-07-10 thomas arg.diff_context = diff_context;
3146 795b88cb 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
3147 795b88cb 2023-07-10 thomas arg.force_text_diff = force_text_diff;
3148 795b88cb 2023-07-10 thomas arg.diffstat = dsa;
3149 795b88cb 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3150 795b88cb 2023-07-10 thomas arg.outfile = outfile;
3151 795b88cb 2023-07-10 thomas arg.lines = NULL;
3152 795b88cb 2023-07-10 thomas arg.nlines = 0;
3153 795b88cb 2023-07-10 thomas while (path[0] == '/')
3154 795b88cb 2023-07-10 thomas path++;
3155 795b88cb 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3156 795b88cb 2023-07-10 thomas got_diff_blob_output_unidiff, &arg, 1);
3157 795b88cb 2023-07-10 thomas done:
3158 795b88cb 2023-07-10 thomas if (tree1)
3159 795b88cb 2023-07-10 thomas got_object_tree_close(tree1);
3160 795b88cb 2023-07-10 thomas if (tree2)
3161 795b88cb 2023-07-10 thomas got_object_tree_close(tree2);
3162 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3163 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3164 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3165 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3166 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3167 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3168 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3169 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3170 795b88cb 2023-07-10 thomas return err;
3171 795b88cb 2023-07-10 thomas }
3172 795b88cb 2023-07-10 thomas
3173 795b88cb 2023-07-10 thomas static const struct got_error *
3174 795b88cb 2023-07-10 thomas get_changed_paths(struct got_pathlist_head *paths,
3175 795b88cb 2023-07-10 thomas struct got_commit_object *commit, struct got_repository *repo,
3176 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *dsa)
3177 795b88cb 2023-07-10 thomas {
3178 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3179 795b88cb 2023-07-10 thomas struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3180 795b88cb 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3181 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3182 795b88cb 2023-07-10 thomas got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3183 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3184 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3185 795b88cb 2023-07-10 thomas
3186 795b88cb 2023-07-10 thomas if (dsa) {
3187 795b88cb 2023-07-10 thomas cb = got_diff_tree_compute_diffstat;
3188 795b88cb 2023-07-10 thomas
3189 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3190 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3191 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3192 795b88cb 2023-07-10 thomas goto done;
3193 795b88cb 2023-07-10 thomas }
3194 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3195 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3196 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3197 795b88cb 2023-07-10 thomas goto done;
3198 795b88cb 2023-07-10 thomas }
3199 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3200 795b88cb 2023-07-10 thomas if (fd1 == -1) {
3201 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3202 795b88cb 2023-07-10 thomas goto done;
3203 795b88cb 2023-07-10 thomas }
3204 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3205 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3206 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3207 795b88cb 2023-07-10 thomas goto done;
3208 795b88cb 2023-07-10 thomas }
3209 795b88cb 2023-07-10 thomas }
3210 795b88cb 2023-07-10 thomas
3211 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3212 795b88cb 2023-07-10 thomas if (qid != NULL) {
3213 795b88cb 2023-07-10 thomas struct got_commit_object *pcommit;
3214 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3215 795b88cb 2023-07-10 thomas &qid->id);
3216 795b88cb 2023-07-10 thomas if (err)
3217 795b88cb 2023-07-10 thomas return err;
3218 795b88cb 2023-07-10 thomas
3219 795b88cb 2023-07-10 thomas tree_id1 = got_object_id_dup(
3220 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
3221 795b88cb 2023-07-10 thomas if (tree_id1 == NULL) {
3222 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3223 795b88cb 2023-07-10 thomas return got_error_from_errno("got_object_id_dup");
3224 795b88cb 2023-07-10 thomas }
3225 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3226 795b88cb 2023-07-10 thomas
3227 795b88cb 2023-07-10 thomas }
3228 795b88cb 2023-07-10 thomas
3229 795b88cb 2023-07-10 thomas if (tree_id1) {
3230 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3231 795b88cb 2023-07-10 thomas if (err)
3232 795b88cb 2023-07-10 thomas goto done;
3233 795b88cb 2023-07-10 thomas }
3234 795b88cb 2023-07-10 thomas
3235 795b88cb 2023-07-10 thomas tree_id2 = got_object_commit_get_tree_id(commit);
3236 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3237 795b88cb 2023-07-10 thomas if (err)
3238 795b88cb 2023-07-10 thomas goto done;
3239 795b88cb 2023-07-10 thomas
3240 795b88cb 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3241 795b88cb 2023-07-10 thomas cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3242 795b88cb 2023-07-10 thomas done:
3243 795b88cb 2023-07-10 thomas if (tree1)
3244 795b88cb 2023-07-10 thomas got_object_tree_close(tree1);
3245 795b88cb 2023-07-10 thomas if (tree2)
3246 795b88cb 2023-07-10 thomas got_object_tree_close(tree2);
3247 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3248 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3249 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3250 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3251 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3252 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3253 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3254 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3255 795b88cb 2023-07-10 thomas free(tree_id1);
3256 795b88cb 2023-07-10 thomas return err;
3257 795b88cb 2023-07-10 thomas }
3258 795b88cb 2023-07-10 thomas
3259 795b88cb 2023-07-10 thomas static const struct got_error *
3260 795b88cb 2023-07-10 thomas print_patch(struct got_commit_object *commit, struct got_object_id *id,
3261 795b88cb 2023-07-10 thomas const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3262 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3263 795b88cb 2023-07-10 thomas {
3264 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3265 795b88cb 2023-07-10 thomas struct got_commit_object *pcommit = NULL;
3266 795b88cb 2023-07-10 thomas char *id_str1 = NULL, *id_str2 = NULL;
3267 795b88cb 2023-07-10 thomas struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3268 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3269 795b88cb 2023-07-10 thomas
3270 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3271 795b88cb 2023-07-10 thomas if (qid != NULL) {
3272 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3273 795b88cb 2023-07-10 thomas &qid->id);
3274 795b88cb 2023-07-10 thomas if (err)
3275 795b88cb 2023-07-10 thomas return err;
3276 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str1, &qid->id);
3277 795b88cb 2023-07-10 thomas if (err)
3278 795b88cb 2023-07-10 thomas goto done;
3279 795b88cb 2023-07-10 thomas }
3280 795b88cb 2023-07-10 thomas
3281 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str2, id);
3282 795b88cb 2023-07-10 thomas if (err)
3283 795b88cb 2023-07-10 thomas goto done;
3284 795b88cb 2023-07-10 thomas
3285 795b88cb 2023-07-10 thomas if (path && path[0] != '\0') {
3286 795b88cb 2023-07-10 thomas int obj_type;
3287 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&obj_id2, repo, commit, path);
3288 795b88cb 2023-07-10 thomas if (err)
3289 795b88cb 2023-07-10 thomas goto done;
3290 795b88cb 2023-07-10 thomas if (pcommit) {
3291 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&obj_id1, repo,
3292 795b88cb 2023-07-10 thomas pcommit, path);
3293 795b88cb 2023-07-10 thomas if (err) {
3294 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3295 795b88cb 2023-07-10 thomas free(obj_id2);
3296 795b88cb 2023-07-10 thomas goto done;
3297 795b88cb 2023-07-10 thomas }
3298 795b88cb 2023-07-10 thomas }
3299 795b88cb 2023-07-10 thomas }
3300 795b88cb 2023-07-10 thomas err = got_object_get_type(&obj_type, repo, obj_id2);
3301 795b88cb 2023-07-10 thomas if (err) {
3302 795b88cb 2023-07-10 thomas free(obj_id2);
3303 795b88cb 2023-07-10 thomas goto done;
3304 795b88cb 2023-07-10 thomas }
3305 795b88cb 2023-07-10 thomas fprintf(outfile,
3306 795b88cb 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3307 795b88cb 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3308 795b88cb 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3309 795b88cb 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3310 795b88cb 2023-07-10 thomas switch (obj_type) {
3311 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
3312 795b88cb 2023-07-10 thomas err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3313 795b88cb 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3314 795b88cb 2023-07-10 thomas break;
3315 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
3316 795b88cb 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, path, diff_context,
3317 795b88cb 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3318 795b88cb 2023-07-10 thomas break;
3319 795b88cb 2023-07-10 thomas default:
3320 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_OBJ_TYPE);
3321 795b88cb 2023-07-10 thomas break;
3322 795b88cb 2023-07-10 thomas }
3323 795b88cb 2023-07-10 thomas free(obj_id1);
3324 795b88cb 2023-07-10 thomas free(obj_id2);
3325 795b88cb 2023-07-10 thomas } else {
3326 795b88cb 2023-07-10 thomas obj_id2 = got_object_commit_get_tree_id(commit);
3327 795b88cb 2023-07-10 thomas if (pcommit)
3328 795b88cb 2023-07-10 thomas obj_id1 = got_object_commit_get_tree_id(pcommit);
3329 795b88cb 2023-07-10 thomas fprintf(outfile,
3330 795b88cb 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3331 795b88cb 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3332 795b88cb 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3333 795b88cb 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3334 795b88cb 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3335 795b88cb 2023-07-10 thomas dsa, repo, outfile);
3336 795b88cb 2023-07-10 thomas }
3337 795b88cb 2023-07-10 thomas done:
3338 795b88cb 2023-07-10 thomas free(id_str1);
3339 795b88cb 2023-07-10 thomas free(id_str2);
3340 795b88cb 2023-07-10 thomas if (pcommit)
3341 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3342 795b88cb 2023-07-10 thomas return err;
3343 795b88cb 2023-07-10 thomas }
3344 795b88cb 2023-07-10 thomas
3345 795b88cb 2023-07-10 thomas static char *
3346 795b88cb 2023-07-10 thomas get_datestr(time_t *time, char *datebuf)
3347 795b88cb 2023-07-10 thomas {
3348 795b88cb 2023-07-10 thomas struct tm mytm, *tm;
3349 795b88cb 2023-07-10 thomas char *p, *s;
3350 795b88cb 2023-07-10 thomas
3351 795b88cb 2023-07-10 thomas tm = gmtime_r(time, &mytm);
3352 795b88cb 2023-07-10 thomas if (tm == NULL)
3353 795b88cb 2023-07-10 thomas return NULL;
3354 795b88cb 2023-07-10 thomas s = asctime_r(tm, datebuf);
3355 795b88cb 2023-07-10 thomas if (s == NULL)
3356 795b88cb 2023-07-10 thomas return NULL;
3357 795b88cb 2023-07-10 thomas p = strchr(s, '\n');
3358 795b88cb 2023-07-10 thomas if (p)
3359 795b88cb 2023-07-10 thomas *p = '\0';
3360 795b88cb 2023-07-10 thomas return s;
3361 795b88cb 2023-07-10 thomas }
3362 795b88cb 2023-07-10 thomas
3363 795b88cb 2023-07-10 thomas static const struct got_error *
3364 795b88cb 2023-07-10 thomas match_commit(int *have_match, struct got_object_id *id,
3365 795b88cb 2023-07-10 thomas struct got_commit_object *commit, regex_t *regex)
3366 795b88cb 2023-07-10 thomas {
3367 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3368 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3369 795b88cb 2023-07-10 thomas char *id_str = NULL, *logmsg = NULL;
3370 795b88cb 2023-07-10 thomas
3371 795b88cb 2023-07-10 thomas *have_match = 0;
3372 795b88cb 2023-07-10 thomas
3373 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3374 795b88cb 2023-07-10 thomas if (err)
3375 795b88cb 2023-07-10 thomas return err;
3376 795b88cb 2023-07-10 thomas
3377 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
3378 795b88cb 2023-07-10 thomas if (err)
3379 795b88cb 2023-07-10 thomas goto done;
3380 795b88cb 2023-07-10 thomas
3381 795b88cb 2023-07-10 thomas if (regexec(regex, got_object_commit_get_author(commit), 1,
3382 795b88cb 2023-07-10 thomas &regmatch, 0) == 0 ||
3383 795b88cb 2023-07-10 thomas regexec(regex, got_object_commit_get_committer(commit), 1,
3384 795b88cb 2023-07-10 thomas &regmatch, 0) == 0 ||
3385 795b88cb 2023-07-10 thomas regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
3386 795b88cb 2023-07-10 thomas regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3387 795b88cb 2023-07-10 thomas *have_match = 1;
3388 795b88cb 2023-07-10 thomas done:
3389 795b88cb 2023-07-10 thomas free(id_str);
3390 795b88cb 2023-07-10 thomas free(logmsg);
3391 795b88cb 2023-07-10 thomas return err;
3392 795b88cb 2023-07-10 thomas }
3393 795b88cb 2023-07-10 thomas
3394 795b88cb 2023-07-10 thomas static void
3395 795b88cb 2023-07-10 thomas match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3396 795b88cb 2023-07-10 thomas regex_t *regex)
3397 795b88cb 2023-07-10 thomas {
3398 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3399 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3400 795b88cb 2023-07-10 thomas
3401 795b88cb 2023-07-10 thomas *have_match = 0;
3402 795b88cb 2023-07-10 thomas
3403 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
3404 795b88cb 2023-07-10 thomas if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3405 795b88cb 2023-07-10 thomas *have_match = 1;
3406 795b88cb 2023-07-10 thomas break;
3407 795b88cb 2023-07-10 thomas }
3408 795b88cb 2023-07-10 thomas }
3409 795b88cb 2023-07-10 thomas }
3410 795b88cb 2023-07-10 thomas
3411 795b88cb 2023-07-10 thomas static const struct got_error *
3412 795b88cb 2023-07-10 thomas match_patch(int *have_match, struct got_commit_object *commit,
3413 795b88cb 2023-07-10 thomas struct got_object_id *id, const char *path, int diff_context,
3414 795b88cb 2023-07-10 thomas struct got_repository *repo, regex_t *regex, FILE *f)
3415 795b88cb 2023-07-10 thomas {
3416 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3417 795b88cb 2023-07-10 thomas char *line = NULL;
3418 795b88cb 2023-07-10 thomas size_t linesize = 0;
3419 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3420 795b88cb 2023-07-10 thomas
3421 795b88cb 2023-07-10 thomas *have_match = 0;
3422 795b88cb 2023-07-10 thomas
3423 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(f);
3424 795b88cb 2023-07-10 thomas if (err)
3425 795b88cb 2023-07-10 thomas return err;
3426 795b88cb 2023-07-10 thomas
3427 795b88cb 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3428 795b88cb 2023-07-10 thomas if (err)
3429 795b88cb 2023-07-10 thomas goto done;
3430 795b88cb 2023-07-10 thomas
3431 795b88cb 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1) {
3432 795b88cb 2023-07-10 thomas err = got_error_from_errno("fseeko");
3433 795b88cb 2023-07-10 thomas goto done;
3434 795b88cb 2023-07-10 thomas }
3435 795b88cb 2023-07-10 thomas
3436 795b88cb 2023-07-10 thomas while (getline(&line, &linesize, f) != -1) {
3437 795b88cb 2023-07-10 thomas if (regexec(regex, line, 1, &regmatch, 0) == 0) {
3438 795b88cb 2023-07-10 thomas *have_match = 1;
3439 795b88cb 2023-07-10 thomas break;
3440 795b88cb 2023-07-10 thomas }
3441 795b88cb 2023-07-10 thomas }
3442 795b88cb 2023-07-10 thomas done:
3443 795b88cb 2023-07-10 thomas free(line);
3444 795b88cb 2023-07-10 thomas return err;
3445 795b88cb 2023-07-10 thomas }
3446 795b88cb 2023-07-10 thomas
3447 795b88cb 2023-07-10 thomas #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3448 795b88cb 2023-07-10 thomas
3449 795b88cb 2023-07-10 thomas static const struct got_error*
3450 795b88cb 2023-07-10 thomas build_refs_str(char **refs_str, struct got_reflist_head *refs,
3451 795b88cb 2023-07-10 thomas struct got_object_id *id, struct got_repository *repo,
3452 795b88cb 2023-07-10 thomas int local_only)
3453 795b88cb 2023-07-10 thomas {
3454 795b88cb 2023-07-10 thomas static const struct got_error *err = NULL;
3455 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
3456 795b88cb 2023-07-10 thomas char *s;
3457 795b88cb 2023-07-10 thomas const char *name;
3458 795b88cb 2023-07-10 thomas
3459 795b88cb 2023-07-10 thomas *refs_str = NULL;
3460 795b88cb 2023-07-10 thomas
3461 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, refs, entry) {
3462 795b88cb 2023-07-10 thomas struct got_tag_object *tag = NULL;
3463 795b88cb 2023-07-10 thomas struct got_object_id *ref_id;
3464 795b88cb 2023-07-10 thomas int cmp;
3465 795b88cb 2023-07-10 thomas
3466 795b88cb 2023-07-10 thomas name = got_ref_get_name(re->ref);
3467 795b88cb 2023-07-10 thomas if (strcmp(name, GOT_REF_HEAD) == 0)
3468 795b88cb 2023-07-10 thomas continue;
3469 795b88cb 2023-07-10 thomas if (strncmp(name, "refs/", 5) == 0)
3470 795b88cb 2023-07-10 thomas name += 5;
3471 795b88cb 2023-07-10 thomas if (strncmp(name, "got/", 4) == 0)
3472 795b88cb 2023-07-10 thomas continue;
3473 795b88cb 2023-07-10 thomas if (strncmp(name, "heads/", 6) == 0)
3474 795b88cb 2023-07-10 thomas name += 6;
3475 795b88cb 2023-07-10 thomas if (strncmp(name, "remotes/", 8) == 0) {
3476 795b88cb 2023-07-10 thomas if (local_only)
3477 795b88cb 2023-07-10 thomas continue;
3478 795b88cb 2023-07-10 thomas name += 8;
3479 795b88cb 2023-07-10 thomas s = strstr(name, "/" GOT_REF_HEAD);
3480 795b88cb 2023-07-10 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
3481 795b88cb 2023-07-10 thomas continue;
3482 795b88cb 2023-07-10 thomas }
3483 795b88cb 2023-07-10 thomas err = got_ref_resolve(&ref_id, repo, re->ref);
3484 795b88cb 2023-07-10 thomas if (err)
3485 795b88cb 2023-07-10 thomas break;
3486 795b88cb 2023-07-10 thomas if (strncmp(name, "tags/", 5) == 0) {
3487 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, ref_id);
3488 795b88cb 2023-07-10 thomas if (err) {
3489 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
3490 795b88cb 2023-07-10 thomas free(ref_id);
3491 795b88cb 2023-07-10 thomas break;
3492 795b88cb 2023-07-10 thomas }
3493 795b88cb 2023-07-10 thomas /* Ref points at something other than a tag. */
3494 795b88cb 2023-07-10 thomas err = NULL;
3495 795b88cb 2023-07-10 thomas tag = NULL;
3496 795b88cb 2023-07-10 thomas }
3497 795b88cb 2023-07-10 thomas }
3498 795b88cb 2023-07-10 thomas cmp = got_object_id_cmp(tag ?
3499 795b88cb 2023-07-10 thomas got_object_tag_get_object_id(tag) : ref_id, id);
3500 795b88cb 2023-07-10 thomas free(ref_id);
3501 795b88cb 2023-07-10 thomas if (tag)
3502 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
3503 795b88cb 2023-07-10 thomas if (cmp != 0)
3504 795b88cb 2023-07-10 thomas continue;
3505 795b88cb 2023-07-10 thomas s = *refs_str;
3506 795b88cb 2023-07-10 thomas if (asprintf(refs_str, "%s%s%s", s ? s : "",
3507 795b88cb 2023-07-10 thomas s ? ", " : "", name) == -1) {
3508 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
3509 795b88cb 2023-07-10 thomas free(s);
3510 795b88cb 2023-07-10 thomas *refs_str = NULL;
3511 795b88cb 2023-07-10 thomas break;
3512 795b88cb 2023-07-10 thomas }
3513 795b88cb 2023-07-10 thomas free(s);
3514 795b88cb 2023-07-10 thomas }
3515 795b88cb 2023-07-10 thomas
3516 795b88cb 2023-07-10 thomas return err;
3517 795b88cb 2023-07-10 thomas }
3518 795b88cb 2023-07-10 thomas
3519 795b88cb 2023-07-10 thomas static const struct got_error *
3520 795b88cb 2023-07-10 thomas print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3521 795b88cb 2023-07-10 thomas struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3522 795b88cb 2023-07-10 thomas {
3523 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3524 795b88cb 2023-07-10 thomas char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3525 795b88cb 2023-07-10 thomas char *comma, *s, *nl;
3526 795b88cb 2023-07-10 thomas struct got_reflist_head *refs;
3527 795b88cb 2023-07-10 thomas char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3528 795b88cb 2023-07-10 thomas struct tm tm;
3529 795b88cb 2023-07-10 thomas time_t committer_time;
3530 795b88cb 2023-07-10 thomas
3531 795b88cb 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3532 795b88cb 2023-07-10 thomas if (refs) {
3533 795b88cb 2023-07-10 thomas err = build_refs_str(&ref_str, refs, id, repo, 1);
3534 795b88cb 2023-07-10 thomas if (err)
3535 795b88cb 2023-07-10 thomas return err;
3536 795b88cb 2023-07-10 thomas
3537 795b88cb 2023-07-10 thomas /* Display the first matching ref only. */
3538 795b88cb 2023-07-10 thomas if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3539 795b88cb 2023-07-10 thomas *comma = '\0';
3540 795b88cb 2023-07-10 thomas }
3541 795b88cb 2023-07-10 thomas
3542 795b88cb 2023-07-10 thomas if (ref_str == NULL) {
3543 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3544 795b88cb 2023-07-10 thomas if (err)
3545 795b88cb 2023-07-10 thomas return err;
3546 795b88cb 2023-07-10 thomas }
3547 795b88cb 2023-07-10 thomas
3548 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
3549 795b88cb 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL) {
3550 795b88cb 2023-07-10 thomas err = got_error_from_errno("gmtime_r");
3551 795b88cb 2023-07-10 thomas goto done;
3552 795b88cb 2023-07-10 thomas }
3553 795b88cb 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
3554 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
3555 795b88cb 2023-07-10 thomas goto done;
3556 795b88cb 2023-07-10 thomas }
3557 795b88cb 2023-07-10 thomas
3558 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
3559 795b88cb 2023-07-10 thomas if (err)
3560 795b88cb 2023-07-10 thomas goto done;
3561 795b88cb 2023-07-10 thomas
3562 795b88cb 2023-07-10 thomas s = logmsg0;
3563 795b88cb 2023-07-10 thomas while (isspace((unsigned char)s[0]))
3564 795b88cb 2023-07-10 thomas s++;
3565 795b88cb 2023-07-10 thomas
3566 795b88cb 2023-07-10 thomas nl = strchr(s, '\n');
3567 795b88cb 2023-07-10 thomas if (nl) {
3568 795b88cb 2023-07-10 thomas *nl = '\0';
3569 795b88cb 2023-07-10 thomas }
3570 795b88cb 2023-07-10 thomas
3571 795b88cb 2023-07-10 thomas if (ref_str)
3572 795b88cb 2023-07-10 thomas printf("%s%-7s %s\n", datebuf, ref_str, s);
3573 795b88cb 2023-07-10 thomas else
3574 795b88cb 2023-07-10 thomas printf("%s%.7s %s\n", datebuf, id_str, s);
3575 795b88cb 2023-07-10 thomas
3576 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
3577 795b88cb 2023-07-10 thomas err = got_error_from_errno("fflush");
3578 795b88cb 2023-07-10 thomas done:
3579 795b88cb 2023-07-10 thomas free(id_str);
3580 795b88cb 2023-07-10 thomas free(ref_str);
3581 795b88cb 2023-07-10 thomas free(logmsg0);
3582 795b88cb 2023-07-10 thomas return err;
3583 795b88cb 2023-07-10 thomas }
3584 795b88cb 2023-07-10 thomas
3585 795b88cb 2023-07-10 thomas static const struct got_error *
3586 795b88cb 2023-07-10 thomas print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
3587 795b88cb 2023-07-10 thomas {
3588 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3589 795b88cb 2023-07-10 thomas
3590 795b88cb 2023-07-10 thomas if (header != NULL)
3591 795b88cb 2023-07-10 thomas printf("%s\n", header);
3592 795b88cb 2023-07-10 thomas
3593 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
3594 795b88cb 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
3595 795b88cb 2023-07-10 thomas int pad = dsa->max_path_len - pe->path_len + 1;
3596 795b88cb 2023-07-10 thomas
3597 795b88cb 2023-07-10 thomas printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
3598 795b88cb 2023-07-10 thomas ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
3599 795b88cb 2023-07-10 thomas }
3600 795b88cb 2023-07-10 thomas printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
3601 795b88cb 2023-07-10 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
3602 795b88cb 2023-07-10 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
3603 795b88cb 2023-07-10 thomas
3604 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0)
3605 795b88cb 2023-07-10 thomas return got_error_from_errno("fflush");
3606 795b88cb 2023-07-10 thomas
3607 795b88cb 2023-07-10 thomas return NULL;
3608 795b88cb 2023-07-10 thomas }
3609 795b88cb 2023-07-10 thomas
3610 795b88cb 2023-07-10 thomas static const struct got_error *
3611 795b88cb 2023-07-10 thomas printfile(FILE *f)
3612 795b88cb 2023-07-10 thomas {
3613 795b88cb 2023-07-10 thomas char buf[8192];
3614 795b88cb 2023-07-10 thomas size_t r;
3615 795b88cb 2023-07-10 thomas
3616 795b88cb 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1)
3617 795b88cb 2023-07-10 thomas return got_error_from_errno("fseek");
3618 795b88cb 2023-07-10 thomas
3619 795b88cb 2023-07-10 thomas for (;;) {
3620 795b88cb 2023-07-10 thomas r = fread(buf, 1, sizeof(buf), f);
3621 795b88cb 2023-07-10 thomas if (r == 0) {
3622 795b88cb 2023-07-10 thomas if (ferror(f))
3623 795b88cb 2023-07-10 thomas return got_error_from_errno("fread");
3624 795b88cb 2023-07-10 thomas if (feof(f))
3625 795b88cb 2023-07-10 thomas break;
3626 795b88cb 2023-07-10 thomas }
3627 795b88cb 2023-07-10 thomas if (fwrite(buf, 1, r, stdout) != r)
3628 795b88cb 2023-07-10 thomas return got_ferror(stdout, GOT_ERR_IO);
3629 795b88cb 2023-07-10 thomas }
3630 795b88cb 2023-07-10 thomas
3631 795b88cb 2023-07-10 thomas return NULL;
3632 795b88cb 2023-07-10 thomas }
3633 795b88cb 2023-07-10 thomas
3634 795b88cb 2023-07-10 thomas static const struct got_error *
3635 795b88cb 2023-07-10 thomas print_commit(struct got_commit_object *commit, struct got_object_id *id,
3636 795b88cb 2023-07-10 thomas struct got_repository *repo, const char *path,
3637 795b88cb 2023-07-10 thomas struct got_pathlist_head *changed_paths,
3638 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
3639 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
3640 795b88cb 2023-07-10 thomas const char *prefix)
3641 795b88cb 2023-07-10 thomas {
3642 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3643 795b88cb 2023-07-10 thomas FILE *f = NULL;
3644 795b88cb 2023-07-10 thomas char *id_str, *datestr, *logmsg0, *logmsg, *line;
3645 795b88cb 2023-07-10 thomas char datebuf[26];
3646 795b88cb 2023-07-10 thomas time_t committer_time;
3647 795b88cb 2023-07-10 thomas const char *author, *committer;
3648 795b88cb 2023-07-10 thomas char *refs_str = NULL;
3649 795b88cb 2023-07-10 thomas
3650 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3651 795b88cb 2023-07-10 thomas if (err)
3652 795b88cb 2023-07-10 thomas return err;
3653 795b88cb 2023-07-10 thomas
3654 795b88cb 2023-07-10 thomas if (custom_refs_str == NULL) {
3655 795b88cb 2023-07-10 thomas struct got_reflist_head *refs;
3656 795b88cb 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3657 795b88cb 2023-07-10 thomas if (refs) {
3658 795b88cb 2023-07-10 thomas err = build_refs_str(&refs_str, refs, id, repo, 0);
3659 795b88cb 2023-07-10 thomas if (err)
3660 795b88cb 2023-07-10 thomas goto done;
3661 795b88cb 2023-07-10 thomas }
3662 795b88cb 2023-07-10 thomas }
3663 795b88cb 2023-07-10 thomas
3664 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
3665 795b88cb 2023-07-10 thomas if (custom_refs_str)
3666 795b88cb 2023-07-10 thomas printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
3667 795b88cb 2023-07-10 thomas custom_refs_str);
3668 795b88cb 2023-07-10 thomas else
3669 795b88cb 2023-07-10 thomas printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
3670 795b88cb 2023-07-10 thomas refs_str ? " (" : "", refs_str ? refs_str : "",
3671 795b88cb 2023-07-10 thomas refs_str ? ")" : "");
3672 795b88cb 2023-07-10 thomas free(id_str);
3673 795b88cb 2023-07-10 thomas id_str = NULL;
3674 795b88cb 2023-07-10 thomas free(refs_str);
3675 795b88cb 2023-07-10 thomas refs_str = NULL;
3676 795b88cb 2023-07-10 thomas printf("from: %s\n", got_object_commit_get_author(commit));
3677 795b88cb 2023-07-10 thomas author = got_object_commit_get_author(commit);
3678 795b88cb 2023-07-10 thomas committer = got_object_commit_get_committer(commit);
3679 795b88cb 2023-07-10 thomas if (strcmp(author, committer) != 0)
3680 795b88cb 2023-07-10 thomas printf("via: %s\n", committer);
3681 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
3682 795b88cb 2023-07-10 thomas datestr = get_datestr(&committer_time, datebuf);
3683 795b88cb 2023-07-10 thomas if (datestr)
3684 795b88cb 2023-07-10 thomas printf("date: %s UTC\n", datestr);
3685 795b88cb 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 1) {
3686 795b88cb 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
3687 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3688 795b88cb 2023-07-10 thomas int n = 1;
3689 795b88cb 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3690 795b88cb 2023-07-10 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3691 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, &qid->id);
3692 795b88cb 2023-07-10 thomas if (err)
3693 795b88cb 2023-07-10 thomas goto done;
3694 795b88cb 2023-07-10 thomas printf("parent %d: %s\n", n++, id_str);
3695 795b88cb 2023-07-10 thomas free(id_str);
3696 795b88cb 2023-07-10 thomas id_str = NULL;
3697 795b88cb 2023-07-10 thomas }
3698 795b88cb 2023-07-10 thomas }
3699 795b88cb 2023-07-10 thomas
3700 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
3701 795b88cb 2023-07-10 thomas if (err)
3702 795b88cb 2023-07-10 thomas goto done;
3703 795b88cb 2023-07-10 thomas
3704 795b88cb 2023-07-10 thomas logmsg = logmsg0;
3705 795b88cb 2023-07-10 thomas do {
3706 795b88cb 2023-07-10 thomas line = strsep(&logmsg, "\n");
3707 795b88cb 2023-07-10 thomas if (line)
3708 795b88cb 2023-07-10 thomas printf(" %s\n", line);
3709 795b88cb 2023-07-10 thomas } while (line);
3710 795b88cb 2023-07-10 thomas free(logmsg0);
3711 795b88cb 2023-07-10 thomas
3712 795b88cb 2023-07-10 thomas if (changed_paths && diffstat == NULL) {
3713 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3714 795b88cb 2023-07-10 thomas
3715 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
3716 795b88cb 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
3717 795b88cb 2023-07-10 thomas
3718 795b88cb 2023-07-10 thomas printf(" %c %s\n", cp->status, pe->path);
3719 795b88cb 2023-07-10 thomas }
3720 795b88cb 2023-07-10 thomas printf("\n");
3721 795b88cb 2023-07-10 thomas }
3722 795b88cb 2023-07-10 thomas if (show_patch) {
3723 795b88cb 2023-07-10 thomas if (diffstat) {
3724 795b88cb 2023-07-10 thomas f = got_opentemp();
3725 795b88cb 2023-07-10 thomas if (f == NULL) {
3726 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3727 795b88cb 2023-07-10 thomas goto done;
3728 795b88cb 2023-07-10 thomas }
3729 795b88cb 2023-07-10 thomas }
3730 795b88cb 2023-07-10 thomas
3731 795b88cb 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, diffstat,
3732 795b88cb 2023-07-10 thomas repo, diffstat == NULL ? stdout : f);
3733 795b88cb 2023-07-10 thomas if (err)
3734 795b88cb 2023-07-10 thomas goto done;
3735 795b88cb 2023-07-10 thomas }
3736 795b88cb 2023-07-10 thomas if (diffstat) {
3737 795b88cb 2023-07-10 thomas err = print_diffstat(diffstat, NULL);
3738 795b88cb 2023-07-10 thomas if (err)
3739 795b88cb 2023-07-10 thomas goto done;
3740 795b88cb 2023-07-10 thomas if (show_patch) {
3741 795b88cb 2023-07-10 thomas err = printfile(f);
3742 795b88cb 2023-07-10 thomas if (err)
3743 795b88cb 2023-07-10 thomas goto done;
3744 795b88cb 2023-07-10 thomas }
3745 795b88cb 2023-07-10 thomas }
3746 795b88cb 2023-07-10 thomas if (show_patch)
3747 795b88cb 2023-07-10 thomas printf("\n");
3748 795b88cb 2023-07-10 thomas
3749 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
3750 795b88cb 2023-07-10 thomas err = got_error_from_errno("fflush");
3751 795b88cb 2023-07-10 thomas done:
3752 795b88cb 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
3753 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3754 795b88cb 2023-07-10 thomas free(id_str);
3755 795b88cb 2023-07-10 thomas free(refs_str);
3756 795b88cb 2023-07-10 thomas return err;
3757 795b88cb 2023-07-10 thomas }
3758 795b88cb 2023-07-10 thomas
3759 795b88cb 2023-07-10 thomas static const struct got_error *
3760 795b88cb 2023-07-10 thomas print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3761 795b88cb 2023-07-10 thomas struct got_repository *repo, const char *path, int show_changed_paths,
3762 795b88cb 2023-07-10 thomas int show_diffstat, int show_patch, const char *search_pattern,
3763 795b88cb 2023-07-10 thomas int diff_context, int limit, int log_branches, int reverse_display_order,
3764 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, int one_line,
3765 795b88cb 2023-07-10 thomas FILE *tmpfile)
3766 795b88cb 2023-07-10 thomas {
3767 795b88cb 2023-07-10 thomas const struct got_error *err;
3768 795b88cb 2023-07-10 thomas struct got_commit_graph *graph;
3769 795b88cb 2023-07-10 thomas regex_t regex;
3770 795b88cb 2023-07-10 thomas int have_match;
3771 795b88cb 2023-07-10 thomas struct got_object_id_queue reversed_commits;
3772 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3773 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
3774 795b88cb 2023-07-10 thomas struct got_pathlist_head changed_paths;
3775 795b88cb 2023-07-10 thomas
3776 795b88cb 2023-07-10 thomas STAILQ_INIT(&reversed_commits);
3777 795b88cb 2023-07-10 thomas TAILQ_INIT(&changed_paths);
3778 795b88cb 2023-07-10 thomas
3779 795b88cb 2023-07-10 thomas if (search_pattern && regcomp(&regex, search_pattern,
3780 795b88cb 2023-07-10 thomas REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3781 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_REGEX, search_pattern);
3782 795b88cb 2023-07-10 thomas
3783 795b88cb 2023-07-10 thomas err = got_commit_graph_open(&graph, path, !log_branches);
3784 795b88cb 2023-07-10 thomas if (err)
3785 795b88cb 2023-07-10 thomas return err;
3786 0279329d 2024-03-30 thomas err = got_commit_graph_bfsort(graph, root_id, repo,
3787 795b88cb 2023-07-10 thomas check_cancelled, NULL);
3788 795b88cb 2023-07-10 thomas if (err)
3789 795b88cb 2023-07-10 thomas goto done;
3790 795b88cb 2023-07-10 thomas for (;;) {
3791 795b88cb 2023-07-10 thomas struct got_object_id id;
3792 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3793 795b88cb 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3794 795b88cb 2023-07-10 thomas
3795 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
3796 795b88cb 2023-07-10 thomas break;
3797 795b88cb 2023-07-10 thomas
3798 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
3799 795b88cb 2023-07-10 thomas check_cancelled, NULL);
3800 795b88cb 2023-07-10 thomas if (err) {
3801 795b88cb 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
3802 795b88cb 2023-07-10 thomas err = NULL;
3803 795b88cb 2023-07-10 thomas break;
3804 795b88cb 2023-07-10 thomas }
3805 795b88cb 2023-07-10 thomas
3806 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, &id);
3807 795b88cb 2023-07-10 thomas if (err)
3808 795b88cb 2023-07-10 thomas break;
3809 795b88cb 2023-07-10 thomas
3810 795b88cb 2023-07-10 thomas if ((show_changed_paths || (show_diffstat && !show_patch))
3811 795b88cb 2023-07-10 thomas && !reverse_display_order) {
3812 795b88cb 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit, repo,
3813 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL);
3814 795b88cb 2023-07-10 thomas if (err)
3815 795b88cb 2023-07-10 thomas break;
3816 795b88cb 2023-07-10 thomas }
3817 795b88cb 2023-07-10 thomas
3818 795b88cb 2023-07-10 thomas if (search_pattern) {
3819 795b88cb 2023-07-10 thomas err = match_commit(&have_match, &id, commit, &regex);
3820 795b88cb 2023-07-10 thomas if (err) {
3821 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3822 795b88cb 2023-07-10 thomas break;
3823 795b88cb 2023-07-10 thomas }
3824 795b88cb 2023-07-10 thomas if (have_match == 0 && show_changed_paths)
3825 795b88cb 2023-07-10 thomas match_changed_paths(&have_match,
3826 795b88cb 2023-07-10 thomas &changed_paths, &regex);
3827 795b88cb 2023-07-10 thomas if (have_match == 0 && show_patch) {
3828 795b88cb 2023-07-10 thomas err = match_patch(&have_match, commit, &id,
3829 795b88cb 2023-07-10 thomas path, diff_context, repo, &regex, tmpfile);
3830 795b88cb 2023-07-10 thomas if (err)
3831 795b88cb 2023-07-10 thomas break;
3832 795b88cb 2023-07-10 thomas }
3833 795b88cb 2023-07-10 thomas if (have_match == 0) {
3834 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3835 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths,
3836 795b88cb 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
3837 795b88cb 2023-07-10 thomas continue;
3838 795b88cb 2023-07-10 thomas }
3839 795b88cb 2023-07-10 thomas }
3840 795b88cb 2023-07-10 thomas
3841 795b88cb 2023-07-10 thomas if (reverse_display_order) {
3842 795b88cb 2023-07-10 thomas err = got_object_qid_alloc(&qid, &id);
3843 795b88cb 2023-07-10 thomas if (err)
3844 795b88cb 2023-07-10 thomas break;
3845 795b88cb 2023-07-10 thomas STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3846 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3847 795b88cb 2023-07-10 thomas } else {
3848 795b88cb 2023-07-10 thomas if (one_line)
3849 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, &id,
3850 795b88cb 2023-07-10 thomas repo, refs_idmap);
3851 795b88cb 2023-07-10 thomas else
3852 795b88cb 2023-07-10 thomas err = print_commit(commit, &id, repo, path,
3853 795b88cb 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
3854 795b88cb 2023-07-10 thomas &changed_paths : NULL,
3855 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
3856 795b88cb 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
3857 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3858 795b88cb 2023-07-10 thomas if (err)
3859 795b88cb 2023-07-10 thomas break;
3860 795b88cb 2023-07-10 thomas }
3861 795b88cb 2023-07-10 thomas if ((limit && --limit == 0) ||
3862 795b88cb 2023-07-10 thomas (end_id && got_object_id_cmp(&id, end_id) == 0))
3863 795b88cb 2023-07-10 thomas break;
3864 795b88cb 2023-07-10 thomas
3865 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3866 795b88cb 2023-07-10 thomas }
3867 795b88cb 2023-07-10 thomas if (reverse_display_order) {
3868 795b88cb 2023-07-10 thomas STAILQ_FOREACH(qid, &reversed_commits, entry) {
3869 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3870 795b88cb 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3871 795b88cb 2023-07-10 thomas
3872 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo,
3873 795b88cb 2023-07-10 thomas &qid->id);
3874 795b88cb 2023-07-10 thomas if (err)
3875 795b88cb 2023-07-10 thomas break;
3876 795b88cb 2023-07-10 thomas if (show_changed_paths ||
3877 795b88cb 2023-07-10 thomas (show_diffstat && !show_patch)) {
3878 795b88cb 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit,
3879 795b88cb 2023-07-10 thomas repo, show_diffstat ? &dsa : NULL);
3880 795b88cb 2023-07-10 thomas if (err)
3881 795b88cb 2023-07-10 thomas break;
3882 795b88cb 2023-07-10 thomas }
3883 795b88cb 2023-07-10 thomas if (one_line)
3884 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, &qid->id,
3885 795b88cb 2023-07-10 thomas repo, refs_idmap);
3886 795b88cb 2023-07-10 thomas else
3887 795b88cb 2023-07-10 thomas err = print_commit(commit, &qid->id, repo, path,
3888 795b88cb 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
3889 795b88cb 2023-07-10 thomas &changed_paths : NULL,
3890 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
3891 795b88cb 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
3892 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3893 795b88cb 2023-07-10 thomas if (err)
3894 795b88cb 2023-07-10 thomas break;
3895 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3896 795b88cb 2023-07-10 thomas }
3897 795b88cb 2023-07-10 thomas }
3898 795b88cb 2023-07-10 thomas done:
3899 795b88cb 2023-07-10 thomas while (!STAILQ_EMPTY(&reversed_commits)) {
3900 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(&reversed_commits);
3901 795b88cb 2023-07-10 thomas STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3902 795b88cb 2023-07-10 thomas got_object_qid_free(qid);
3903 795b88cb 2023-07-10 thomas }
3904 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3905 795b88cb 2023-07-10 thomas if (search_pattern)
3906 795b88cb 2023-07-10 thomas regfree(&regex);
3907 795b88cb 2023-07-10 thomas got_commit_graph_close(graph);
3908 795b88cb 2023-07-10 thomas return err;
3909 795b88cb 2023-07-10 thomas }
3910 795b88cb 2023-07-10 thomas
3911 795b88cb 2023-07-10 thomas __dead static void
3912 795b88cb 2023-07-10 thomas usage_log(void)
3913 795b88cb 2023-07-10 thomas {
3914 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
3915 795b88cb 2023-07-10 thomas "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
3916 795b88cb 2023-07-10 thomas "[path]\n", getprogname());
3917 795b88cb 2023-07-10 thomas exit(1);
3918 795b88cb 2023-07-10 thomas }
3919 795b88cb 2023-07-10 thomas
3920 795b88cb 2023-07-10 thomas static int
3921 795b88cb 2023-07-10 thomas get_default_log_limit(void)
3922 795b88cb 2023-07-10 thomas {
3923 795b88cb 2023-07-10 thomas const char *got_default_log_limit;
3924 795b88cb 2023-07-10 thomas long long n;
3925 795b88cb 2023-07-10 thomas const char *errstr;
3926 795b88cb 2023-07-10 thomas
3927 795b88cb 2023-07-10 thomas got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3928 795b88cb 2023-07-10 thomas if (got_default_log_limit == NULL)
3929 795b88cb 2023-07-10 thomas return 0;
3930 795b88cb 2023-07-10 thomas n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3931 795b88cb 2023-07-10 thomas if (errstr != NULL)
3932 795b88cb 2023-07-10 thomas return 0;
3933 795b88cb 2023-07-10 thomas return n;
3934 795b88cb 2023-07-10 thomas }
3935 795b88cb 2023-07-10 thomas
3936 795b88cb 2023-07-10 thomas static const struct got_error *
3937 795b88cb 2023-07-10 thomas cmd_log(int argc, char *argv[])
3938 795b88cb 2023-07-10 thomas {
3939 795b88cb 2023-07-10 thomas const struct got_error *error;
3940 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
3941 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
3942 795b88cb 2023-07-10 thomas struct got_object_id *start_id = NULL, *end_id = NULL;
3943 795b88cb 2023-07-10 thomas char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3944 795b88cb 2023-07-10 thomas const char *start_commit = NULL, *end_commit = NULL;
3945 795b88cb 2023-07-10 thomas const char *search_pattern = NULL;
3946 795b88cb 2023-07-10 thomas int diff_context = -1, ch;
3947 795b88cb 2023-07-10 thomas int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3948 795b88cb 2023-07-10 thomas int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
3949 795b88cb 2023-07-10 thomas const char *errstr;
3950 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
3951 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
3952 795b88cb 2023-07-10 thomas FILE *tmpfile = NULL;
3953 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
3954 795b88cb 2023-07-10 thomas
3955 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
3956 795b88cb 2023-07-10 thomas
3957 795b88cb 2023-07-10 thomas #ifndef PROFILE
3958 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3959 795b88cb 2023-07-10 thomas NULL)
3960 795b88cb 2023-07-10 thomas == -1)
3961 795b88cb 2023-07-10 thomas err(1, "pledge");
3962 795b88cb 2023-07-10 thomas #endif
3963 795b88cb 2023-07-10 thomas
3964 795b88cb 2023-07-10 thomas limit = get_default_log_limit();
3965 795b88cb 2023-07-10 thomas
3966 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
3967 795b88cb 2023-07-10 thomas switch (ch) {
3968 795b88cb 2023-07-10 thomas case 'b':
3969 795b88cb 2023-07-10 thomas log_branches = 1;
3970 795b88cb 2023-07-10 thomas break;
3971 795b88cb 2023-07-10 thomas case 'C':
3972 795b88cb 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3973 795b88cb 2023-07-10 thomas &errstr);
3974 795b88cb 2023-07-10 thomas if (errstr != NULL)
3975 795b88cb 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
3976 795b88cb 2023-07-10 thomas errstr, optarg);
3977 795b88cb 2023-07-10 thomas break;
3978 795b88cb 2023-07-10 thomas case 'c':
3979 795b88cb 2023-07-10 thomas start_commit = optarg;
3980 795b88cb 2023-07-10 thomas break;
3981 795b88cb 2023-07-10 thomas case 'd':
3982 795b88cb 2023-07-10 thomas show_diffstat = 1;
3983 795b88cb 2023-07-10 thomas break;
3984 795b88cb 2023-07-10 thomas case 'l':
3985 795b88cb 2023-07-10 thomas limit = strtonum(optarg, 0, INT_MAX, &errstr);
3986 795b88cb 2023-07-10 thomas if (errstr != NULL)
3987 795b88cb 2023-07-10 thomas errx(1, "number of commits is %s: %s",
3988 795b88cb 2023-07-10 thomas errstr, optarg);
3989 795b88cb 2023-07-10 thomas break;
3990 795b88cb 2023-07-10 thomas case 'P':
3991 795b88cb 2023-07-10 thomas show_changed_paths = 1;
3992 795b88cb 2023-07-10 thomas break;
3993 795b88cb 2023-07-10 thomas case 'p':
3994 795b88cb 2023-07-10 thomas show_patch = 1;
3995 795b88cb 2023-07-10 thomas break;
3996 795b88cb 2023-07-10 thomas case 'R':
3997 795b88cb 2023-07-10 thomas reverse_display_order = 1;
3998 795b88cb 2023-07-10 thomas break;
3999 795b88cb 2023-07-10 thomas case 'r':
4000 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
4001 795b88cb 2023-07-10 thomas if (repo_path == NULL)
4002 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
4003 795b88cb 2023-07-10 thomas optarg);
4004 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4005 795b88cb 2023-07-10 thomas break;
4006 795b88cb 2023-07-10 thomas case 'S':
4007 795b88cb 2023-07-10 thomas search_pattern = optarg;
4008 795b88cb 2023-07-10 thomas break;
4009 795b88cb 2023-07-10 thomas case 's':
4010 795b88cb 2023-07-10 thomas one_line = 1;
4011 795b88cb 2023-07-10 thomas break;
4012 795b88cb 2023-07-10 thomas case 'x':
4013 795b88cb 2023-07-10 thomas end_commit = optarg;
4014 795b88cb 2023-07-10 thomas break;
4015 795b88cb 2023-07-10 thomas default:
4016 795b88cb 2023-07-10 thomas usage_log();
4017 795b88cb 2023-07-10 thomas /* NOTREACHED */
4018 795b88cb 2023-07-10 thomas }
4019 795b88cb 2023-07-10 thomas }
4020 795b88cb 2023-07-10 thomas
4021 795b88cb 2023-07-10 thomas argc -= optind;
4022 795b88cb 2023-07-10 thomas argv += optind;
4023 795b88cb 2023-07-10 thomas
4024 795b88cb 2023-07-10 thomas if (diff_context == -1)
4025 795b88cb 2023-07-10 thomas diff_context = 3;
4026 795b88cb 2023-07-10 thomas else if (!show_patch)
4027 795b88cb 2023-07-10 thomas errx(1, "-C requires -p");
4028 795b88cb 2023-07-10 thomas
4029 795b88cb 2023-07-10 thomas if (one_line && (show_patch || show_changed_paths || show_diffstat))
4030 795b88cb 2023-07-10 thomas errx(1, "cannot use -s with -d, -p or -P");
4031 795b88cb 2023-07-10 thomas
4032 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
4033 795b88cb 2023-07-10 thomas if (cwd == NULL) {
4034 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
4035 795b88cb 2023-07-10 thomas goto done;
4036 795b88cb 2023-07-10 thomas }
4037 795b88cb 2023-07-10 thomas
4038 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4039 795b88cb 2023-07-10 thomas if (error != NULL)
4040 795b88cb 2023-07-10 thomas goto done;
4041 795b88cb 2023-07-10 thomas
4042 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4043 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
4044 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4045 795b88cb 2023-07-10 thomas goto done;
4046 795b88cb 2023-07-10 thomas error = NULL;
4047 795b88cb 2023-07-10 thomas }
4048 795b88cb 2023-07-10 thomas
4049 795b88cb 2023-07-10 thomas if (argc == 1) {
4050 795b88cb 2023-07-10 thomas if (worktree) {
4051 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&path, worktree,
4052 795b88cb 2023-07-10 thomas argv[0]);
4053 795b88cb 2023-07-10 thomas if (error)
4054 795b88cb 2023-07-10 thomas goto done;
4055 795b88cb 2023-07-10 thomas } else {
4056 795b88cb 2023-07-10 thomas path = strdup(argv[0]);
4057 795b88cb 2023-07-10 thomas if (path == NULL) {
4058 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4059 795b88cb 2023-07-10 thomas goto done;
4060 795b88cb 2023-07-10 thomas }
4061 795b88cb 2023-07-10 thomas }
4062 795b88cb 2023-07-10 thomas } else if (argc != 0)
4063 795b88cb 2023-07-10 thomas usage_log();
4064 795b88cb 2023-07-10 thomas
4065 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4066 795b88cb 2023-07-10 thomas repo_path = worktree ?
4067 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4068 795b88cb 2023-07-10 thomas }
4069 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4070 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4071 795b88cb 2023-07-10 thomas goto done;
4072 795b88cb 2023-07-10 thomas }
4073 795b88cb 2023-07-10 thomas
4074 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4075 795b88cb 2023-07-10 thomas if (error != NULL)
4076 795b88cb 2023-07-10 thomas goto done;
4077 795b88cb 2023-07-10 thomas
4078 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4079 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4080 795b88cb 2023-07-10 thomas if (error)
4081 795b88cb 2023-07-10 thomas goto done;
4082 795b88cb 2023-07-10 thomas
4083 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4084 795b88cb 2023-07-10 thomas if (error)
4085 795b88cb 2023-07-10 thomas goto done;
4086 795b88cb 2023-07-10 thomas
4087 795b88cb 2023-07-10 thomas error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4088 795b88cb 2023-07-10 thomas if (error)
4089 795b88cb 2023-07-10 thomas goto done;
4090 795b88cb 2023-07-10 thomas
4091 795b88cb 2023-07-10 thomas if (start_commit == NULL) {
4092 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
4093 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
4094 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
4095 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
4096 795b88cb 2023-07-10 thomas : GOT_REF_HEAD, 0);
4097 795b88cb 2023-07-10 thomas if (error != NULL)
4098 795b88cb 2023-07-10 thomas goto done;
4099 795b88cb 2023-07-10 thomas error = got_ref_resolve(&start_id, repo, head_ref);
4100 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
4101 795b88cb 2023-07-10 thomas if (error != NULL)
4102 795b88cb 2023-07-10 thomas goto done;
4103 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo,
4104 795b88cb 2023-07-10 thomas start_id);
4105 795b88cb 2023-07-10 thomas if (error != NULL)
4106 795b88cb 2023-07-10 thomas goto done;
4107 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4108 795b88cb 2023-07-10 thomas } else {
4109 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&start_id, NULL,
4110 795b88cb 2023-07-10 thomas start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4111 795b88cb 2023-07-10 thomas if (error != NULL)
4112 795b88cb 2023-07-10 thomas goto done;
4113 795b88cb 2023-07-10 thomas }
4114 795b88cb 2023-07-10 thomas if (end_commit != NULL) {
4115 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&end_id, NULL,
4116 795b88cb 2023-07-10 thomas end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4117 795b88cb 2023-07-10 thomas if (error != NULL)
4118 795b88cb 2023-07-10 thomas goto done;
4119 795b88cb 2023-07-10 thomas }
4120 795b88cb 2023-07-10 thomas
4121 795b88cb 2023-07-10 thomas if (worktree) {
4122 795b88cb 2023-07-10 thomas /*
4123 795b88cb 2023-07-10 thomas * If a path was specified on the command line it was resolved
4124 795b88cb 2023-07-10 thomas * to a path in the work tree above. Prepend the work tree's
4125 795b88cb 2023-07-10 thomas * path prefix to obtain the corresponding in-repository path.
4126 795b88cb 2023-07-10 thomas */
4127 795b88cb 2023-07-10 thomas if (path) {
4128 795b88cb 2023-07-10 thomas const char *prefix;
4129 795b88cb 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4130 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4131 795b88cb 2023-07-10 thomas (path[0] != '\0') ? "/" : "", path) == -1) {
4132 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4133 795b88cb 2023-07-10 thomas goto done;
4134 795b88cb 2023-07-10 thomas }
4135 795b88cb 2023-07-10 thomas }
4136 795b88cb 2023-07-10 thomas } else
4137 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo,
4138 795b88cb 2023-07-10 thomas path ? path : "");
4139 795b88cb 2023-07-10 thomas if (error != NULL)
4140 795b88cb 2023-07-10 thomas goto done;
4141 795b88cb 2023-07-10 thomas if (in_repo_path) {
4142 795b88cb 2023-07-10 thomas free(path);
4143 795b88cb 2023-07-10 thomas path = in_repo_path;
4144 795b88cb 2023-07-10 thomas }
4145 795b88cb 2023-07-10 thomas
4146 795b88cb 2023-07-10 thomas if (worktree) {
4147 795b88cb 2023-07-10 thomas /* Release work tree lock. */
4148 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4149 795b88cb 2023-07-10 thomas worktree = NULL;
4150 795b88cb 2023-07-10 thomas }
4151 795b88cb 2023-07-10 thomas
4152 795b88cb 2023-07-10 thomas if (search_pattern && show_patch) {
4153 795b88cb 2023-07-10 thomas tmpfile = got_opentemp();
4154 795b88cb 2023-07-10 thomas if (tmpfile == NULL) {
4155 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4156 795b88cb 2023-07-10 thomas goto done;
4157 795b88cb 2023-07-10 thomas }
4158 795b88cb 2023-07-10 thomas }
4159 795b88cb 2023-07-10 thomas
4160 795b88cb 2023-07-10 thomas error = print_commits(start_id, end_id, repo, path ? path : "",
4161 795b88cb 2023-07-10 thomas show_changed_paths, show_diffstat, show_patch, search_pattern,
4162 795b88cb 2023-07-10 thomas diff_context, limit, log_branches, reverse_display_order,
4163 795b88cb 2023-07-10 thomas refs_idmap, one_line, tmpfile);
4164 795b88cb 2023-07-10 thomas done:
4165 795b88cb 2023-07-10 thomas free(path);
4166 795b88cb 2023-07-10 thomas free(repo_path);
4167 795b88cb 2023-07-10 thomas free(cwd);
4168 609a7400 2023-07-17 thomas free(start_id);
4169 609a7400 2023-07-17 thomas free(end_id);
4170 795b88cb 2023-07-10 thomas if (worktree)
4171 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4172 795b88cb 2023-07-10 thomas if (repo) {
4173 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4174 795b88cb 2023-07-10 thomas if (error == NULL)
4175 795b88cb 2023-07-10 thomas error = close_err;
4176 795b88cb 2023-07-10 thomas }
4177 795b88cb 2023-07-10 thomas if (pack_fds) {
4178 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
4179 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4180 795b88cb 2023-07-10 thomas if (error == NULL)
4181 795b88cb 2023-07-10 thomas error = pack_err;
4182 795b88cb 2023-07-10 thomas }
4183 795b88cb 2023-07-10 thomas if (refs_idmap)
4184 795b88cb 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
4185 795b88cb 2023-07-10 thomas if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4186 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4187 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
4188 795b88cb 2023-07-10 thomas return error;
4189 795b88cb 2023-07-10 thomas }
4190 795b88cb 2023-07-10 thomas
4191 795b88cb 2023-07-10 thomas __dead static void
4192 795b88cb 2023-07-10 thomas usage_diff(void)
4193 795b88cb 2023-07-10 thomas {
4194 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4195 795b88cb 2023-07-10 thomas "[-r repository-path] [object1 object2 | path ...]\n",
4196 795b88cb 2023-07-10 thomas getprogname());
4197 795b88cb 2023-07-10 thomas exit(1);
4198 795b88cb 2023-07-10 thomas }
4199 795b88cb 2023-07-10 thomas
4200 795b88cb 2023-07-10 thomas struct print_diff_arg {
4201 795b88cb 2023-07-10 thomas struct got_repository *repo;
4202 795b88cb 2023-07-10 thomas struct got_worktree *worktree;
4203 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat;
4204 795b88cb 2023-07-10 thomas int diff_context;
4205 795b88cb 2023-07-10 thomas const char *id_str;
4206 795b88cb 2023-07-10 thomas int header_shown;
4207 795b88cb 2023-07-10 thomas int diff_staged;
4208 795b88cb 2023-07-10 thomas enum got_diff_algorithm diff_algo;
4209 795b88cb 2023-07-10 thomas int ignore_whitespace;
4210 795b88cb 2023-07-10 thomas int force_text_diff;
4211 795b88cb 2023-07-10 thomas FILE *f1;
4212 795b88cb 2023-07-10 thomas FILE *f2;
4213 795b88cb 2023-07-10 thomas FILE *outfile;
4214 795b88cb 2023-07-10 thomas };
4215 795b88cb 2023-07-10 thomas
4216 795b88cb 2023-07-10 thomas /*
4217 795b88cb 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
4218 795b88cb 2023-07-10 thomas * it as content to the diff engine.
4219 795b88cb 2023-07-10 thomas */
4220 795b88cb 2023-07-10 thomas static const struct got_error *
4221 795b88cb 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4222 795b88cb 2023-07-10 thomas const char *abspath)
4223 795b88cb 2023-07-10 thomas {
4224 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4225 795b88cb 2023-07-10 thomas char target_path[PATH_MAX];
4226 795b88cb 2023-07-10 thomas ssize_t target_len, outlen;
4227 795b88cb 2023-07-10 thomas
4228 795b88cb 2023-07-10 thomas *fd = -1;
4229 795b88cb 2023-07-10 thomas
4230 795b88cb 2023-07-10 thomas if (dirfd != -1) {
4231 795b88cb 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4232 795b88cb 2023-07-10 thomas if (target_len == -1)
4233 795b88cb 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
4234 795b88cb 2023-07-10 thomas } else {
4235 795b88cb 2023-07-10 thomas target_len = readlink(abspath, target_path, PATH_MAX);
4236 795b88cb 2023-07-10 thomas if (target_len == -1)
4237 795b88cb 2023-07-10 thomas return got_error_from_errno2("readlink", abspath);
4238 795b88cb 2023-07-10 thomas }
4239 795b88cb 2023-07-10 thomas
4240 795b88cb 2023-07-10 thomas *fd = got_opentempfd();
4241 795b88cb 2023-07-10 thomas if (*fd == -1)
4242 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
4243 795b88cb 2023-07-10 thomas
4244 795b88cb 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
4245 795b88cb 2023-07-10 thomas if (outlen == -1) {
4246 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4247 795b88cb 2023-07-10 thomas goto done;
4248 795b88cb 2023-07-10 thomas }
4249 795b88cb 2023-07-10 thomas
4250 795b88cb 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
4251 795b88cb 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
4252 795b88cb 2023-07-10 thomas goto done;
4253 795b88cb 2023-07-10 thomas }
4254 795b88cb 2023-07-10 thomas done:
4255 795b88cb 2023-07-10 thomas if (err) {
4256 795b88cb 2023-07-10 thomas close(*fd);
4257 795b88cb 2023-07-10 thomas *fd = -1;
4258 795b88cb 2023-07-10 thomas }
4259 795b88cb 2023-07-10 thomas return err;
4260 795b88cb 2023-07-10 thomas }
4261 795b88cb 2023-07-10 thomas
4262 795b88cb 2023-07-10 thomas static const struct got_error *
4263 795b88cb 2023-07-10 thomas print_diff(void *arg, unsigned char status, unsigned char staged_status,
4264 795b88cb 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
4265 795b88cb 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4266 795b88cb 2023-07-10 thomas int dirfd, const char *de_name)
4267 795b88cb 2023-07-10 thomas {
4268 795b88cb 2023-07-10 thomas struct print_diff_arg *a = arg;
4269 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4270 795b88cb 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
4271 795b88cb 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
4272 795b88cb 2023-07-10 thomas FILE *f2 = NULL;
4273 795b88cb 2023-07-10 thomas char *abspath = NULL, *label1 = NULL;
4274 795b88cb 2023-07-10 thomas struct stat sb;
4275 795b88cb 2023-07-10 thomas off_t size1 = 0;
4276 795b88cb 2023-07-10 thomas int f2_exists = 0;
4277 795b88cb 2023-07-10 thomas
4278 795b88cb 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
4279 795b88cb 2023-07-10 thomas
4280 795b88cb 2023-07-10 thomas if (a->diff_staged) {
4281 795b88cb 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
4282 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
4283 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
4284 795b88cb 2023-07-10 thomas return NULL;
4285 795b88cb 2023-07-10 thomas } else {
4286 795b88cb 2023-07-10 thomas if (staged_status == GOT_STATUS_DELETE)
4287 795b88cb 2023-07-10 thomas return NULL;
4288 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
4289 795b88cb 2023-07-10 thomas return got_error_set_errno(ENOENT, path);
4290 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
4291 795b88cb 2023-07-10 thomas status != GOT_STATUS_ADD &&
4292 795b88cb 2023-07-10 thomas status != GOT_STATUS_DELETE &&
4293 795b88cb 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
4294 795b88cb 2023-07-10 thomas return NULL;
4295 795b88cb 2023-07-10 thomas }
4296 795b88cb 2023-07-10 thomas
4297 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(a->f1);
4298 795b88cb 2023-07-10 thomas if (err)
4299 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4300 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(a->f2);
4301 795b88cb 2023-07-10 thomas if (err)
4302 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4303 795b88cb 2023-07-10 thomas
4304 795b88cb 2023-07-10 thomas if (!a->header_shown) {
4305 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "diff %s%s\n",
4306 795b88cb 2023-07-10 thomas a->diff_staged ? "-s " : "",
4307 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree)) < 0) {
4308 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4309 795b88cb 2023-07-10 thomas goto done;
4310 795b88cb 2023-07-10 thomas }
4311 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4312 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4313 795b88cb 2023-07-10 thomas goto done;
4314 795b88cb 2023-07-10 thomas }
4315 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "path + %s%s\n",
4316 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree),
4317 795b88cb 2023-07-10 thomas a->diff_staged ? " (staged changes)" : "") < 0) {
4318 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4319 795b88cb 2023-07-10 thomas goto done;
4320 795b88cb 2023-07-10 thomas }
4321 795b88cb 2023-07-10 thomas a->header_shown = 1;
4322 795b88cb 2023-07-10 thomas }
4323 795b88cb 2023-07-10 thomas
4324 795b88cb 2023-07-10 thomas if (a->diff_staged) {
4325 795b88cb 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
4326 795b88cb 2023-07-10 thomas switch (staged_status) {
4327 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
4328 795b88cb 2023-07-10 thomas label1 = path;
4329 795b88cb 2023-07-10 thomas label2 = path;
4330 795b88cb 2023-07-10 thomas break;
4331 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
4332 795b88cb 2023-07-10 thomas label2 = path;
4333 795b88cb 2023-07-10 thomas break;
4334 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
4335 795b88cb 2023-07-10 thomas label1 = path;
4336 795b88cb 2023-07-10 thomas break;
4337 795b88cb 2023-07-10 thomas default:
4338 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
4339 795b88cb 2023-07-10 thomas }
4340 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4341 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4342 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4343 795b88cb 2023-07-10 thomas goto done;
4344 795b88cb 2023-07-10 thomas }
4345 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
4346 795b88cb 2023-07-10 thomas if (fd2 == -1) {
4347 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4348 795b88cb 2023-07-10 thomas goto done;
4349 795b88cb 2023-07-10 thomas }
4350 795b88cb 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4351 795b88cb 2023-07-10 thomas fd1, fd2, blob_id, staged_blob_id, label1, label2,
4352 795b88cb 2023-07-10 thomas a->diff_algo, a->diff_context, a->ignore_whitespace,
4353 795b88cb 2023-07-10 thomas a->force_text_diff, a->diffstat, a->repo, a->outfile);
4354 795b88cb 2023-07-10 thomas goto done;
4355 795b88cb 2023-07-10 thomas }
4356 795b88cb 2023-07-10 thomas
4357 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4358 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4359 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4360 795b88cb 2023-07-10 thomas goto done;
4361 795b88cb 2023-07-10 thomas }
4362 795b88cb 2023-07-10 thomas
4363 795b88cb 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
4364 795b88cb 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
4365 795b88cb 2023-07-10 thomas char *id_str;
4366 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4367 795b88cb 2023-07-10 thomas 8192, fd1);
4368 795b88cb 2023-07-10 thomas if (err)
4369 795b88cb 2023-07-10 thomas goto done;
4370 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
4371 795b88cb 2023-07-10 thomas if (err)
4372 795b88cb 2023-07-10 thomas goto done;
4373 795b88cb 2023-07-10 thomas if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4374 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
4375 795b88cb 2023-07-10 thomas free(id_str);
4376 795b88cb 2023-07-10 thomas goto done;
4377 795b88cb 2023-07-10 thomas }
4378 795b88cb 2023-07-10 thomas free(id_str);
4379 795b88cb 2023-07-10 thomas } else if (status != GOT_STATUS_ADD) {
4380 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4381 795b88cb 2023-07-10 thomas fd1);
4382 795b88cb 2023-07-10 thomas if (err)
4383 795b88cb 2023-07-10 thomas goto done;
4384 795b88cb 2023-07-10 thomas }
4385 795b88cb 2023-07-10 thomas
4386 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_DELETE) {
4387 795b88cb 2023-07-10 thomas if (asprintf(&abspath, "%s/%s",
4388 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree), path) == -1) {
4389 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
4390 795b88cb 2023-07-10 thomas goto done;
4391 795b88cb 2023-07-10 thomas }
4392 795b88cb 2023-07-10 thomas
4393 795b88cb 2023-07-10 thomas if (dirfd != -1) {
4394 795b88cb 2023-07-10 thomas fd = openat(dirfd, de_name,
4395 795b88cb 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4396 795b88cb 2023-07-10 thomas if (fd == -1) {
4397 795b88cb 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
4398 795b88cb 2023-07-10 thomas err = got_error_from_errno2("openat",
4399 795b88cb 2023-07-10 thomas abspath);
4400 795b88cb 2023-07-10 thomas goto done;
4401 795b88cb 2023-07-10 thomas }
4402 795b88cb 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
4403 795b88cb 2023-07-10 thomas de_name, abspath);
4404 795b88cb 2023-07-10 thomas if (err)
4405 795b88cb 2023-07-10 thomas goto done;
4406 795b88cb 2023-07-10 thomas }
4407 795b88cb 2023-07-10 thomas } else {
4408 795b88cb 2023-07-10 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4409 795b88cb 2023-07-10 thomas if (fd == -1) {
4410 795b88cb 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
4411 795b88cb 2023-07-10 thomas err = got_error_from_errno2("open",
4412 795b88cb 2023-07-10 thomas abspath);
4413 795b88cb 2023-07-10 thomas goto done;
4414 795b88cb 2023-07-10 thomas }
4415 795b88cb 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
4416 795b88cb 2023-07-10 thomas de_name, abspath);
4417 795b88cb 2023-07-10 thomas if (err)
4418 795b88cb 2023-07-10 thomas goto done;
4419 795b88cb 2023-07-10 thomas }
4420 795b88cb 2023-07-10 thomas }
4421 795b88cb 2023-07-10 thomas if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4422 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fstatat", abspath);
4423 795b88cb 2023-07-10 thomas goto done;
4424 795b88cb 2023-07-10 thomas }
4425 795b88cb 2023-07-10 thomas f2 = fdopen(fd, "r");
4426 795b88cb 2023-07-10 thomas if (f2 == NULL) {
4427 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fdopen", abspath);
4428 795b88cb 2023-07-10 thomas goto done;
4429 795b88cb 2023-07-10 thomas }
4430 795b88cb 2023-07-10 thomas fd = -1;
4431 795b88cb 2023-07-10 thomas f2_exists = 1;
4432 795b88cb 2023-07-10 thomas }
4433 795b88cb 2023-07-10 thomas
4434 795b88cb 2023-07-10 thomas if (blob1) {
4435 795b88cb 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4436 795b88cb 2023-07-10 thomas a->f1, blob1);
4437 795b88cb 2023-07-10 thomas if (err)
4438 795b88cb 2023-07-10 thomas goto done;
4439 795b88cb 2023-07-10 thomas }
4440 795b88cb 2023-07-10 thomas
4441 795b88cb 2023-07-10 thomas err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4442 795b88cb 2023-07-10 thomas f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4443 795b88cb 2023-07-10 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
4444 795b88cb 2023-07-10 thomas done:
4445 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4446 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4447 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4448 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4449 795b88cb 2023-07-10 thomas if (blob1)
4450 795b88cb 2023-07-10 thomas got_object_blob_close(blob1);
4451 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4452 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4453 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
4454 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
4455 795b88cb 2023-07-10 thomas free(abspath);
4456 795b88cb 2023-07-10 thomas return err;
4457 795b88cb 2023-07-10 thomas }
4458 795b88cb 2023-07-10 thomas
4459 795b88cb 2023-07-10 thomas static const struct got_error *
4460 795b88cb 2023-07-10 thomas cmd_diff(int argc, char *argv[])
4461 795b88cb 2023-07-10 thomas {
4462 795b88cb 2023-07-10 thomas const struct got_error *error;
4463 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
4464 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
4465 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
4466 795b88cb 2023-07-10 thomas const char *commit_args[2] = { NULL, NULL };
4467 795b88cb 2023-07-10 thomas int ncommit_args = 0;
4468 795b88cb 2023-07-10 thomas struct got_object_id *ids[2] = { NULL, NULL };
4469 795b88cb 2023-07-10 thomas char *labels[2] = { NULL, NULL };
4470 795b88cb 2023-07-10 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4471 795b88cb 2023-07-10 thomas int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4472 795b88cb 2023-07-10 thomas int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
4473 795b88cb 2023-07-10 thomas const char *errstr;
4474 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
4475 795b88cb 2023-07-10 thomas struct got_pathlist_head diffstat_paths, paths;
4476 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4477 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
4478 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
4479 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa;
4480 795b88cb 2023-07-10 thomas
4481 795b88cb 2023-07-10 thomas memset(&dsa, 0, sizeof(dsa));
4482 795b88cb 2023-07-10 thomas
4483 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
4484 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
4485 795b88cb 2023-07-10 thomas TAILQ_INIT(&diffstat_paths);
4486 795b88cb 2023-07-10 thomas
4487 795b88cb 2023-07-10 thomas #ifndef PROFILE
4488 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4489 795b88cb 2023-07-10 thomas NULL) == -1)
4490 795b88cb 2023-07-10 thomas err(1, "pledge");
4491 795b88cb 2023-07-10 thomas #endif
4492 795b88cb 2023-07-10 thomas
4493 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
4494 795b88cb 2023-07-10 thomas switch (ch) {
4495 795b88cb 2023-07-10 thomas case 'a':
4496 795b88cb 2023-07-10 thomas force_text_diff = 1;
4497 795b88cb 2023-07-10 thomas break;
4498 795b88cb 2023-07-10 thomas case 'C':
4499 795b88cb 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4500 795b88cb 2023-07-10 thomas &errstr);
4501 795b88cb 2023-07-10 thomas if (errstr != NULL)
4502 795b88cb 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
4503 795b88cb 2023-07-10 thomas errstr, optarg);
4504 795b88cb 2023-07-10 thomas break;
4505 795b88cb 2023-07-10 thomas case 'c':
4506 795b88cb 2023-07-10 thomas if (ncommit_args >= 2)
4507 795b88cb 2023-07-10 thomas errx(1, "too many -c options used");
4508 795b88cb 2023-07-10 thomas commit_args[ncommit_args++] = optarg;
4509 795b88cb 2023-07-10 thomas break;
4510 795b88cb 2023-07-10 thomas case 'd':
4511 795b88cb 2023-07-10 thomas show_diffstat = 1;
4512 795b88cb 2023-07-10 thomas break;
4513 795b88cb 2023-07-10 thomas case 'P':
4514 795b88cb 2023-07-10 thomas force_path = 1;
4515 795b88cb 2023-07-10 thomas break;
4516 795b88cb 2023-07-10 thomas case 'r':
4517 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
4518 795b88cb 2023-07-10 thomas if (repo_path == NULL)
4519 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
4520 795b88cb 2023-07-10 thomas optarg);
4521 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4522 795b88cb 2023-07-10 thomas rflag = 1;
4523 795b88cb 2023-07-10 thomas break;
4524 795b88cb 2023-07-10 thomas case 's':
4525 795b88cb 2023-07-10 thomas diff_staged = 1;
4526 795b88cb 2023-07-10 thomas break;
4527 795b88cb 2023-07-10 thomas case 'w':
4528 795b88cb 2023-07-10 thomas ignore_whitespace = 1;
4529 795b88cb 2023-07-10 thomas break;
4530 795b88cb 2023-07-10 thomas default:
4531 795b88cb 2023-07-10 thomas usage_diff();
4532 795b88cb 2023-07-10 thomas /* NOTREACHED */
4533 795b88cb 2023-07-10 thomas }
4534 795b88cb 2023-07-10 thomas }
4535 795b88cb 2023-07-10 thomas
4536 795b88cb 2023-07-10 thomas argc -= optind;
4537 795b88cb 2023-07-10 thomas argv += optind;
4538 795b88cb 2023-07-10 thomas
4539 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
4540 795b88cb 2023-07-10 thomas if (cwd == NULL) {
4541 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
4542 795b88cb 2023-07-10 thomas goto done;
4543 795b88cb 2023-07-10 thomas }
4544 795b88cb 2023-07-10 thomas
4545 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4546 795b88cb 2023-07-10 thomas if (error != NULL)
4547 795b88cb 2023-07-10 thomas goto done;
4548 795b88cb 2023-07-10 thomas
4549 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4550 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
4551 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4552 795b88cb 2023-07-10 thomas goto done;
4553 795b88cb 2023-07-10 thomas else
4554 795b88cb 2023-07-10 thomas error = NULL;
4555 795b88cb 2023-07-10 thomas if (worktree) {
4556 795b88cb 2023-07-10 thomas repo_path =
4557 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
4558 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4559 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4560 795b88cb 2023-07-10 thomas goto done;
4561 795b88cb 2023-07-10 thomas }
4562 795b88cb 2023-07-10 thomas } else {
4563 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
4564 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4565 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4566 795b88cb 2023-07-10 thomas goto done;
4567 795b88cb 2023-07-10 thomas }
4568 795b88cb 2023-07-10 thomas }
4569 795b88cb 2023-07-10 thomas }
4570 795b88cb 2023-07-10 thomas
4571 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4572 795b88cb 2023-07-10 thomas free(repo_path);
4573 795b88cb 2023-07-10 thomas if (error != NULL)
4574 795b88cb 2023-07-10 thomas goto done;
4575 795b88cb 2023-07-10 thomas
4576 795b88cb 2023-07-10 thomas if (show_diffstat) {
4577 795b88cb 2023-07-10 thomas dsa.paths = &diffstat_paths;
4578 795b88cb 2023-07-10 thomas dsa.force_text = force_text_diff;
4579 795b88cb 2023-07-10 thomas dsa.ignore_ws = ignore_whitespace;
4580 795b88cb 2023-07-10 thomas dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4581 795b88cb 2023-07-10 thomas }
4582 795b88cb 2023-07-10 thomas
4583 795b88cb 2023-07-10 thomas if (rflag || worktree == NULL || ncommit_args > 0) {
4584 795b88cb 2023-07-10 thomas if (force_path) {
4585 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4586 795b88cb 2023-07-10 thomas "-P option can only be used when diffing "
4587 795b88cb 2023-07-10 thomas "a work tree");
4588 795b88cb 2023-07-10 thomas goto done;
4589 795b88cb 2023-07-10 thomas }
4590 795b88cb 2023-07-10 thomas if (diff_staged) {
4591 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4592 795b88cb 2023-07-10 thomas "-s option can only be used when diffing "
4593 795b88cb 2023-07-10 thomas "a work tree");
4594 795b88cb 2023-07-10 thomas goto done;
4595 795b88cb 2023-07-10 thomas }
4596 795b88cb 2023-07-10 thomas }
4597 795b88cb 2023-07-10 thomas
4598 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4599 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4600 795b88cb 2023-07-10 thomas if (error)
4601 795b88cb 2023-07-10 thomas goto done;
4602 795b88cb 2023-07-10 thomas
4603 795b88cb 2023-07-10 thomas if ((!force_path && argc == 2) || ncommit_args > 0) {
4604 795b88cb 2023-07-10 thomas int obj_type = (ncommit_args > 0 ?
4605 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4606 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4607 795b88cb 2023-07-10 thomas NULL);
4608 795b88cb 2023-07-10 thomas if (error)
4609 795b88cb 2023-07-10 thomas goto done;
4610 795b88cb 2023-07-10 thomas for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4611 795b88cb 2023-07-10 thomas const char *arg;
4612 795b88cb 2023-07-10 thomas if (ncommit_args > 0)
4613 795b88cb 2023-07-10 thomas arg = commit_args[i];
4614 795b88cb 2023-07-10 thomas else
4615 795b88cb 2023-07-10 thomas arg = argv[i];
4616 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&ids[i], &labels[i],
4617 795b88cb 2023-07-10 thomas arg, obj_type, &refs, repo);
4618 795b88cb 2023-07-10 thomas if (error) {
4619 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF &&
4620 795b88cb 2023-07-10 thomas error->code != GOT_ERR_NO_OBJ)
4621 795b88cb 2023-07-10 thomas goto done;
4622 795b88cb 2023-07-10 thomas if (ncommit_args > 0)
4623 795b88cb 2023-07-10 thomas goto done;
4624 795b88cb 2023-07-10 thomas error = NULL;
4625 795b88cb 2023-07-10 thomas break;
4626 795b88cb 2023-07-10 thomas }
4627 795b88cb 2023-07-10 thomas }
4628 795b88cb 2023-07-10 thomas }
4629 795b88cb 2023-07-10 thomas
4630 795b88cb 2023-07-10 thomas f1 = got_opentemp();
4631 795b88cb 2023-07-10 thomas if (f1 == NULL) {
4632 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4633 795b88cb 2023-07-10 thomas goto done;
4634 795b88cb 2023-07-10 thomas }
4635 795b88cb 2023-07-10 thomas
4636 795b88cb 2023-07-10 thomas f2 = got_opentemp();
4637 795b88cb 2023-07-10 thomas if (f2 == NULL) {
4638 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4639 795b88cb 2023-07-10 thomas goto done;
4640 795b88cb 2023-07-10 thomas }
4641 795b88cb 2023-07-10 thomas
4642 795b88cb 2023-07-10 thomas outfile = got_opentemp();
4643 795b88cb 2023-07-10 thomas if (outfile == NULL) {
4644 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4645 795b88cb 2023-07-10 thomas goto done;
4646 795b88cb 2023-07-10 thomas }
4647 795b88cb 2023-07-10 thomas
4648 795b88cb 2023-07-10 thomas if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4649 795b88cb 2023-07-10 thomas struct print_diff_arg arg;
4650 795b88cb 2023-07-10 thomas char *id_str;
4651 795b88cb 2023-07-10 thomas
4652 795b88cb 2023-07-10 thomas if (worktree == NULL) {
4653 795b88cb 2023-07-10 thomas if (argc == 2 && ids[0] == NULL) {
4654 795b88cb 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4655 795b88cb 2023-07-10 thomas goto done;
4656 795b88cb 2023-07-10 thomas } else if (argc == 2 && ids[1] == NULL) {
4657 795b88cb 2023-07-10 thomas error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4658 795b88cb 2023-07-10 thomas goto done;
4659 795b88cb 2023-07-10 thomas } else if (argc > 0) {
4660 795b88cb 2023-07-10 thomas error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4661 795b88cb 2023-07-10 thomas "%s", "specified paths cannot be resolved");
4662 795b88cb 2023-07-10 thomas goto done;
4663 795b88cb 2023-07-10 thomas } else {
4664 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_NOT_WORKTREE);
4665 795b88cb 2023-07-10 thomas goto done;
4666 795b88cb 2023-07-10 thomas }
4667 795b88cb 2023-07-10 thomas }
4668 795b88cb 2023-07-10 thomas
4669 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
4670 795b88cb 2023-07-10 thomas worktree);
4671 795b88cb 2023-07-10 thomas if (error)
4672 795b88cb 2023-07-10 thomas goto done;
4673 795b88cb 2023-07-10 thomas
4674 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str,
4675 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
4676 795b88cb 2023-07-10 thomas if (error)
4677 795b88cb 2023-07-10 thomas goto done;
4678 795b88cb 2023-07-10 thomas arg.repo = repo;
4679 795b88cb 2023-07-10 thomas arg.worktree = worktree;
4680 795b88cb 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4681 795b88cb 2023-07-10 thomas arg.diff_context = diff_context;
4682 795b88cb 2023-07-10 thomas arg.id_str = id_str;
4683 795b88cb 2023-07-10 thomas arg.header_shown = 0;
4684 795b88cb 2023-07-10 thomas arg.diff_staged = diff_staged;
4685 795b88cb 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
4686 795b88cb 2023-07-10 thomas arg.force_text_diff = force_text_diff;
4687 795b88cb 2023-07-10 thomas arg.diffstat = show_diffstat ? &dsa : NULL;
4688 795b88cb 2023-07-10 thomas arg.f1 = f1;
4689 795b88cb 2023-07-10 thomas arg.f2 = f2;
4690 795b88cb 2023-07-10 thomas arg.outfile = outfile;
4691 795b88cb 2023-07-10 thomas
4692 795b88cb 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, 0,
4693 795b88cb 2023-07-10 thomas print_diff, &arg, check_cancelled, NULL);
4694 795b88cb 2023-07-10 thomas free(id_str);
4695 795b88cb 2023-07-10 thomas if (error)
4696 795b88cb 2023-07-10 thomas goto done;
4697 795b88cb 2023-07-10 thomas
4698 795b88cb 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
4699 795b88cb 2023-07-10 thomas char *header;
4700 795b88cb 2023-07-10 thomas
4701 795b88cb 2023-07-10 thomas if (asprintf(&header, "diffstat %s%s",
4702 795b88cb 2023-07-10 thomas diff_staged ? "-s " : "",
4703 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree)) == -1) {
4704 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4705 795b88cb 2023-07-10 thomas goto done;
4706 795b88cb 2023-07-10 thomas }
4707 795b88cb 2023-07-10 thomas
4708 795b88cb 2023-07-10 thomas error = print_diffstat(&dsa, header);
4709 795b88cb 2023-07-10 thomas free(header);
4710 795b88cb 2023-07-10 thomas if (error)
4711 795b88cb 2023-07-10 thomas goto done;
4712 795b88cb 2023-07-10 thomas }
4713 795b88cb 2023-07-10 thomas
4714 795b88cb 2023-07-10 thomas error = printfile(outfile);
4715 795b88cb 2023-07-10 thomas goto done;
4716 795b88cb 2023-07-10 thomas }
4717 795b88cb 2023-07-10 thomas
4718 795b88cb 2023-07-10 thomas if (ncommit_args == 1) {
4719 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
4720 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, ids[0]);
4721 795b88cb 2023-07-10 thomas if (error)
4722 795b88cb 2023-07-10 thomas goto done;
4723 795b88cb 2023-07-10 thomas
4724 795b88cb 2023-07-10 thomas labels[1] = labels[0];
4725 795b88cb 2023-07-10 thomas ids[1] = ids[0];
4726 795b88cb 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 0) {
4727 795b88cb 2023-07-10 thomas const struct got_object_id_queue *pids;
4728 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
4729 795b88cb 2023-07-10 thomas pids = got_object_commit_get_parent_ids(commit);
4730 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(pids);
4731 795b88cb 2023-07-10 thomas ids[0] = got_object_id_dup(&pid->id);
4732 795b88cb 2023-07-10 thomas if (ids[0] == NULL) {
4733 795b88cb 2023-07-10 thomas error = got_error_from_errno(
4734 795b88cb 2023-07-10 thomas "got_object_id_dup");
4735 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4736 795b88cb 2023-07-10 thomas goto done;
4737 795b88cb 2023-07-10 thomas }
4738 795b88cb 2023-07-10 thomas error = got_object_id_str(&labels[0], ids[0]);
4739 795b88cb 2023-07-10 thomas if (error) {
4740 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4741 795b88cb 2023-07-10 thomas goto done;
4742 795b88cb 2023-07-10 thomas }
4743 795b88cb 2023-07-10 thomas } else {
4744 795b88cb 2023-07-10 thomas ids[0] = NULL;
4745 795b88cb 2023-07-10 thomas labels[0] = strdup("/dev/null");
4746 795b88cb 2023-07-10 thomas if (labels[0] == NULL) {
4747 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4748 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4749 795b88cb 2023-07-10 thomas goto done;
4750 795b88cb 2023-07-10 thomas }
4751 795b88cb 2023-07-10 thomas }
4752 795b88cb 2023-07-10 thomas
4753 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4754 795b88cb 2023-07-10 thomas }
4755 795b88cb 2023-07-10 thomas
4756 795b88cb 2023-07-10 thomas if (ncommit_args == 0 && argc > 2) {
4757 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
4758 795b88cb 2023-07-10 thomas "path arguments cannot be used when diffing two objects");
4759 795b88cb 2023-07-10 thomas goto done;
4760 795b88cb 2023-07-10 thomas }
4761 795b88cb 2023-07-10 thomas
4762 795b88cb 2023-07-10 thomas if (ids[0]) {
4763 795b88cb 2023-07-10 thomas error = got_object_get_type(&type1, repo, ids[0]);
4764 795b88cb 2023-07-10 thomas if (error)
4765 795b88cb 2023-07-10 thomas goto done;
4766 795b88cb 2023-07-10 thomas }
4767 795b88cb 2023-07-10 thomas
4768 795b88cb 2023-07-10 thomas error = got_object_get_type(&type2, repo, ids[1]);
4769 795b88cb 2023-07-10 thomas if (error)
4770 795b88cb 2023-07-10 thomas goto done;
4771 795b88cb 2023-07-10 thomas if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4772 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
4773 795b88cb 2023-07-10 thomas goto done;
4774 795b88cb 2023-07-10 thomas }
4775 795b88cb 2023-07-10 thomas if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
4776 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_OBJ_TYPE,
4777 795b88cb 2023-07-10 thomas "path arguments cannot be used when diffing blobs");
4778 795b88cb 2023-07-10 thomas goto done;
4779 795b88cb 2023-07-10 thomas }
4780 795b88cb 2023-07-10 thomas
4781 795b88cb 2023-07-10 thomas for (i = 0; ncommit_args > 0 && i < argc; i++) {
4782 795b88cb 2023-07-10 thomas char *in_repo_path;
4783 795b88cb 2023-07-10 thomas struct got_pathlist_entry *new;
4784 795b88cb 2023-07-10 thomas if (worktree) {
4785 795b88cb 2023-07-10 thomas const char *prefix;
4786 795b88cb 2023-07-10 thomas char *p;
4787 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree,
4788 795b88cb 2023-07-10 thomas argv[i]);
4789 795b88cb 2023-07-10 thomas if (error)
4790 795b88cb 2023-07-10 thomas goto done;
4791 795b88cb 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4792 795b88cb 2023-07-10 thomas while (prefix[0] == '/')
4793 795b88cb 2023-07-10 thomas prefix++;
4794 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4795 795b88cb 2023-07-10 thomas (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4796 795b88cb 2023-07-10 thomas p) == -1) {
4797 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4798 795b88cb 2023-07-10 thomas free(p);
4799 795b88cb 2023-07-10 thomas goto done;
4800 795b88cb 2023-07-10 thomas }
4801 795b88cb 2023-07-10 thomas free(p);
4802 795b88cb 2023-07-10 thomas } else {
4803 795b88cb 2023-07-10 thomas char *mapped_path, *s;
4804 795b88cb 2023-07-10 thomas error = got_repo_map_path(&mapped_path, repo, argv[i]);
4805 795b88cb 2023-07-10 thomas if (error)
4806 795b88cb 2023-07-10 thomas goto done;
4807 795b88cb 2023-07-10 thomas s = mapped_path;
4808 795b88cb 2023-07-10 thomas while (s[0] == '/')
4809 795b88cb 2023-07-10 thomas s++;
4810 795b88cb 2023-07-10 thomas in_repo_path = strdup(s);
4811 795b88cb 2023-07-10 thomas if (in_repo_path == NULL) {
4812 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4813 795b88cb 2023-07-10 thomas free(mapped_path);
4814 795b88cb 2023-07-10 thomas goto done;
4815 795b88cb 2023-07-10 thomas }
4816 795b88cb 2023-07-10 thomas free(mapped_path);
4817 795b88cb 2023-07-10 thomas
4818 795b88cb 2023-07-10 thomas }
4819 795b88cb 2023-07-10 thomas error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4820 795b88cb 2023-07-10 thomas if (error || new == NULL /* duplicate */)
4821 795b88cb 2023-07-10 thomas free(in_repo_path);
4822 795b88cb 2023-07-10 thomas if (error)
4823 795b88cb 2023-07-10 thomas goto done;
4824 795b88cb 2023-07-10 thomas }
4825 795b88cb 2023-07-10 thomas
4826 795b88cb 2023-07-10 thomas if (worktree) {
4827 795b88cb 2023-07-10 thomas /* Release work tree lock. */
4828 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4829 795b88cb 2023-07-10 thomas worktree = NULL;
4830 795b88cb 2023-07-10 thomas }
4831 795b88cb 2023-07-10 thomas
4832 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4833 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4834 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
4835 795b88cb 2023-07-10 thomas goto done;
4836 795b88cb 2023-07-10 thomas }
4837 795b88cb 2023-07-10 thomas
4838 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
4839 795b88cb 2023-07-10 thomas if (fd2 == -1) {
4840 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
4841 795b88cb 2023-07-10 thomas goto done;
4842 795b88cb 2023-07-10 thomas }
4843 795b88cb 2023-07-10 thomas
4844 795b88cb 2023-07-10 thomas switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4845 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
4846 795b88cb 2023-07-10 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4847 795b88cb 2023-07-10 thomas fd1, fd2, ids[0], ids[1], NULL, NULL,
4848 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4849 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4850 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4851 795b88cb 2023-07-10 thomas break;
4852 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
4853 795b88cb 2023-07-10 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
4854 795b88cb 2023-07-10 thomas ids[0], ids[1], &paths, "", "",
4855 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4856 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4857 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4858 795b88cb 2023-07-10 thomas break;
4859 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
4860 795b88cb 2023-07-10 thomas fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
4861 795b88cb 2023-07-10 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
4862 795b88cb 2023-07-10 thomas fd1, fd2, ids[0], ids[1], &paths,
4863 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4864 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4865 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4866 795b88cb 2023-07-10 thomas break;
4867 795b88cb 2023-07-10 thomas default:
4868 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
4869 795b88cb 2023-07-10 thomas }
4870 795b88cb 2023-07-10 thomas if (error)
4871 795b88cb 2023-07-10 thomas goto done;
4872 795b88cb 2023-07-10 thomas
4873 795b88cb 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
4874 795b88cb 2023-07-10 thomas char *header = NULL;
4875 795b88cb 2023-07-10 thomas
4876 795b88cb 2023-07-10 thomas if (asprintf(&header, "diffstat %s %s",
4877 795b88cb 2023-07-10 thomas labels[0], labels[1]) == -1) {
4878 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4879 795b88cb 2023-07-10 thomas goto done;
4880 795b88cb 2023-07-10 thomas }
4881 795b88cb 2023-07-10 thomas
4882 795b88cb 2023-07-10 thomas error = print_diffstat(&dsa, header);
4883 795b88cb 2023-07-10 thomas free(header);
4884 795b88cb 2023-07-10 thomas if (error)
4885 795b88cb 2023-07-10 thomas goto done;
4886 795b88cb 2023-07-10 thomas }
4887 795b88cb 2023-07-10 thomas
4888 795b88cb 2023-07-10 thomas error = printfile(outfile);
4889 795b88cb 2023-07-10 thomas
4890 795b88cb 2023-07-10 thomas done:
4891 795b88cb 2023-07-10 thomas free(labels[0]);
4892 795b88cb 2023-07-10 thomas free(labels[1]);
4893 795b88cb 2023-07-10 thomas free(ids[0]);
4894 795b88cb 2023-07-10 thomas free(ids[1]);
4895 795b88cb 2023-07-10 thomas if (worktree)
4896 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4897 795b88cb 2023-07-10 thomas if (repo) {
4898 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4899 795b88cb 2023-07-10 thomas if (error == NULL)
4900 795b88cb 2023-07-10 thomas error = close_err;
4901 795b88cb 2023-07-10 thomas }
4902 795b88cb 2023-07-10 thomas if (pack_fds) {
4903 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
4904 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4905 795b88cb 2023-07-10 thomas if (error == NULL)
4906 795b88cb 2023-07-10 thomas error = pack_err;
4907 795b88cb 2023-07-10 thomas }
4908 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
4909 795b88cb 2023-07-10 thomas got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
4910 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
4911 795b88cb 2023-07-10 thomas if (outfile && fclose(outfile) == EOF && error == NULL)
4912 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4913 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
4914 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4915 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
4916 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4917 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4918 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
4919 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4920 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
4921 795b88cb 2023-07-10 thomas return error;
4922 795b88cb 2023-07-10 thomas }
4923 795b88cb 2023-07-10 thomas
4924 795b88cb 2023-07-10 thomas __dead static void
4925 795b88cb 2023-07-10 thomas usage_blame(void)
4926 795b88cb 2023-07-10 thomas {
4927 795b88cb 2023-07-10 thomas fprintf(stderr,
4928 795b88cb 2023-07-10 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
4929 795b88cb 2023-07-10 thomas getprogname());
4930 795b88cb 2023-07-10 thomas exit(1);
4931 795b88cb 2023-07-10 thomas }
4932 795b88cb 2023-07-10 thomas
4933 795b88cb 2023-07-10 thomas struct blame_line {
4934 795b88cb 2023-07-10 thomas int annotated;
4935 795b88cb 2023-07-10 thomas char *id_str;
4936 795b88cb 2023-07-10 thomas char *committer;
4937 795b88cb 2023-07-10 thomas char datebuf[11]; /* YYYY-MM-DD + NUL */
4938 795b88cb 2023-07-10 thomas };
4939 795b88cb 2023-07-10 thomas
4940 795b88cb 2023-07-10 thomas struct blame_cb_args {
4941 795b88cb 2023-07-10 thomas struct blame_line *lines;
4942 795b88cb 2023-07-10 thomas int nlines;
4943 795b88cb 2023-07-10 thomas int nlines_prec;
4944 795b88cb 2023-07-10 thomas int lineno_cur;
4945 795b88cb 2023-07-10 thomas off_t *line_offsets;
4946 795b88cb 2023-07-10 thomas FILE *f;
4947 795b88cb 2023-07-10 thomas struct got_repository *repo;
4948 795b88cb 2023-07-10 thomas };
4949 795b88cb 2023-07-10 thomas
4950 795b88cb 2023-07-10 thomas static const struct got_error *
4951 795b88cb 2023-07-10 thomas blame_cb(void *arg, int nlines, int lineno,
4952 795b88cb 2023-07-10 thomas struct got_commit_object *commit, struct got_object_id *id)
4953 795b88cb 2023-07-10 thomas {
4954 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4955 795b88cb 2023-07-10 thomas struct blame_cb_args *a = arg;
4956 795b88cb 2023-07-10 thomas struct blame_line *bline;
4957 795b88cb 2023-07-10 thomas char *line = NULL;
4958 795b88cb 2023-07-10 thomas size_t linesize = 0;
4959 795b88cb 2023-07-10 thomas off_t offset;
4960 795b88cb 2023-07-10 thomas struct tm tm;
4961 795b88cb 2023-07-10 thomas time_t committer_time;
4962 795b88cb 2023-07-10 thomas
4963 795b88cb 2023-07-10 thomas if (nlines != a->nlines ||
4964 795b88cb 2023-07-10 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
4965 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_RANGE);
4966 795b88cb 2023-07-10 thomas
4967 795b88cb 2023-07-10 thomas if (sigint_received)
4968 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ITER_COMPLETED);
4969 795b88cb 2023-07-10 thomas
4970 795b88cb 2023-07-10 thomas if (lineno == -1)
4971 795b88cb 2023-07-10 thomas return NULL; /* no change in this commit */
4972 795b88cb 2023-07-10 thomas
4973 795b88cb 2023-07-10 thomas /* Annotate this line. */
4974 795b88cb 2023-07-10 thomas bline = &a->lines[lineno - 1];
4975 795b88cb 2023-07-10 thomas if (bline->annotated)
4976 795b88cb 2023-07-10 thomas return NULL;
4977 795b88cb 2023-07-10 thomas err = got_object_id_str(&bline->id_str, id);
4978 795b88cb 2023-07-10 thomas if (err)
4979 795b88cb 2023-07-10 thomas return err;
4980 795b88cb 2023-07-10 thomas
4981 795b88cb 2023-07-10 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
4982 795b88cb 2023-07-10 thomas if (bline->committer == NULL) {
4983 795b88cb 2023-07-10 thomas err = got_error_from_errno("strdup");
4984 795b88cb 2023-07-10 thomas goto done;
4985 795b88cb 2023-07-10 thomas }
4986 795b88cb 2023-07-10 thomas
4987 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
4988 795b88cb 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL)
4989 795b88cb 2023-07-10 thomas return got_error_from_errno("gmtime_r");
4990 795b88cb 2023-07-10 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4991 795b88cb 2023-07-10 thomas &tm) == 0) {
4992 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
4993 795b88cb 2023-07-10 thomas goto done;
4994 795b88cb 2023-07-10 thomas }
4995 795b88cb 2023-07-10 thomas bline->annotated = 1;
4996 795b88cb 2023-07-10 thomas
4997 795b88cb 2023-07-10 thomas /* Print lines annotated so far. */
4998 795b88cb 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
4999 795b88cb 2023-07-10 thomas if (!bline->annotated)
5000 795b88cb 2023-07-10 thomas goto done;
5001 795b88cb 2023-07-10 thomas
5002 795b88cb 2023-07-10 thomas offset = a->line_offsets[a->lineno_cur - 1];
5003 795b88cb 2023-07-10 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
5004 795b88cb 2023-07-10 thomas err = got_error_from_errno("fseeko");
5005 795b88cb 2023-07-10 thomas goto done;
5006 795b88cb 2023-07-10 thomas }
5007 795b88cb 2023-07-10 thomas
5008 795b88cb 2023-07-10 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
5009 795b88cb 2023-07-10 thomas char *smallerthan, *at, *nl, *committer;
5010 795b88cb 2023-07-10 thomas size_t len;
5011 795b88cb 2023-07-10 thomas
5012 795b88cb 2023-07-10 thomas if (getline(&line, &linesize, a->f) == -1) {
5013 795b88cb 2023-07-10 thomas if (ferror(a->f))
5014 795b88cb 2023-07-10 thomas err = got_error_from_errno("getline");
5015 795b88cb 2023-07-10 thomas break;
5016 795b88cb 2023-07-10 thomas }
5017 795b88cb 2023-07-10 thomas
5018 795b88cb 2023-07-10 thomas committer = bline->committer;
5019 795b88cb 2023-07-10 thomas smallerthan = strchr(committer, '<');
5020 795b88cb 2023-07-10 thomas if (smallerthan && smallerthan[1] != '\0')
5021 795b88cb 2023-07-10 thomas committer = smallerthan + 1;
5022 795b88cb 2023-07-10 thomas at = strchr(committer, '@');
5023 795b88cb 2023-07-10 thomas if (at)
5024 795b88cb 2023-07-10 thomas *at = '\0';
5025 795b88cb 2023-07-10 thomas len = strlen(committer);
5026 795b88cb 2023-07-10 thomas if (len >= 9)
5027 795b88cb 2023-07-10 thomas committer[8] = '\0';
5028 795b88cb 2023-07-10 thomas
5029 795b88cb 2023-07-10 thomas nl = strchr(line, '\n');
5030 795b88cb 2023-07-10 thomas if (nl)
5031 795b88cb 2023-07-10 thomas *nl = '\0';
5032 795b88cb 2023-07-10 thomas printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5033 795b88cb 2023-07-10 thomas bline->id_str, bline->datebuf, committer, line);
5034 795b88cb 2023-07-10 thomas
5035 795b88cb 2023-07-10 thomas a->lineno_cur++;
5036 795b88cb 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
5037 795b88cb 2023-07-10 thomas }
5038 795b88cb 2023-07-10 thomas done:
5039 795b88cb 2023-07-10 thomas free(line);
5040 795b88cb 2023-07-10 thomas return err;
5041 795b88cb 2023-07-10 thomas }
5042 795b88cb 2023-07-10 thomas
5043 795b88cb 2023-07-10 thomas static const struct got_error *
5044 795b88cb 2023-07-10 thomas cmd_blame(int argc, char *argv[])
5045 795b88cb 2023-07-10 thomas {
5046 795b88cb 2023-07-10 thomas const struct got_error *error;
5047 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5048 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5049 795b88cb 2023-07-10 thomas char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5050 795b88cb 2023-07-10 thomas char *link_target = NULL;
5051 795b88cb 2023-07-10 thomas struct got_object_id *obj_id = NULL;
5052 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5053 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5054 795b88cb 2023-07-10 thomas struct got_blob_object *blob = NULL;
5055 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
5056 795b88cb 2023-07-10 thomas struct blame_cb_args bca;
5057 795b88cb 2023-07-10 thomas int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5058 795b88cb 2023-07-10 thomas off_t filesize;
5059 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5060 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
5061 795b88cb 2023-07-10 thomas
5062 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
5063 795b88cb 2023-07-10 thomas if (fd1 == -1)
5064 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
5065 795b88cb 2023-07-10 thomas
5066 795b88cb 2023-07-10 thomas memset(&bca, 0, sizeof(bca));
5067 795b88cb 2023-07-10 thomas
5068 795b88cb 2023-07-10 thomas #ifndef PROFILE
5069 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5070 795b88cb 2023-07-10 thomas NULL) == -1)
5071 795b88cb 2023-07-10 thomas err(1, "pledge");
5072 795b88cb 2023-07-10 thomas #endif
5073 795b88cb 2023-07-10 thomas
5074 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5075 795b88cb 2023-07-10 thomas switch (ch) {
5076 795b88cb 2023-07-10 thomas case 'c':
5077 795b88cb 2023-07-10 thomas commit_id_str = optarg;
5078 795b88cb 2023-07-10 thomas break;
5079 795b88cb 2023-07-10 thomas case 'r':
5080 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5081 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5082 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
5083 795b88cb 2023-07-10 thomas optarg);
5084 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5085 795b88cb 2023-07-10 thomas break;
5086 795b88cb 2023-07-10 thomas default:
5087 795b88cb 2023-07-10 thomas usage_blame();
5088 795b88cb 2023-07-10 thomas /* NOTREACHED */
5089 795b88cb 2023-07-10 thomas }
5090 795b88cb 2023-07-10 thomas }
5091 795b88cb 2023-07-10 thomas
5092 795b88cb 2023-07-10 thomas argc -= optind;
5093 795b88cb 2023-07-10 thomas argv += optind;
5094 795b88cb 2023-07-10 thomas
5095 795b88cb 2023-07-10 thomas if (argc == 1)
5096 795b88cb 2023-07-10 thomas path = argv[0];
5097 795b88cb 2023-07-10 thomas else
5098 795b88cb 2023-07-10 thomas usage_blame();
5099 795b88cb 2023-07-10 thomas
5100 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5101 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5102 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5103 795b88cb 2023-07-10 thomas goto done;
5104 795b88cb 2023-07-10 thomas }
5105 795b88cb 2023-07-10 thomas
5106 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5107 795b88cb 2023-07-10 thomas if (error != NULL)
5108 795b88cb 2023-07-10 thomas goto done;
5109 795b88cb 2023-07-10 thomas
5110 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5111 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5112 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5113 795b88cb 2023-07-10 thomas goto done;
5114 795b88cb 2023-07-10 thomas else
5115 795b88cb 2023-07-10 thomas error = NULL;
5116 795b88cb 2023-07-10 thomas if (worktree) {
5117 795b88cb 2023-07-10 thomas repo_path =
5118 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5119 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5120 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5121 795b88cb 2023-07-10 thomas if (error)
5122 795b88cb 2023-07-10 thomas goto done;
5123 795b88cb 2023-07-10 thomas }
5124 795b88cb 2023-07-10 thomas } else {
5125 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
5126 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5127 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5128 795b88cb 2023-07-10 thomas goto done;
5129 795b88cb 2023-07-10 thomas }
5130 795b88cb 2023-07-10 thomas }
5131 795b88cb 2023-07-10 thomas }
5132 795b88cb 2023-07-10 thomas
5133 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5134 795b88cb 2023-07-10 thomas if (error != NULL)
5135 795b88cb 2023-07-10 thomas goto done;
5136 795b88cb 2023-07-10 thomas
5137 795b88cb 2023-07-10 thomas if (worktree) {
5138 795b88cb 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5139 795b88cb 2023-07-10 thomas char *p;
5140 795b88cb 2023-07-10 thomas
5141 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5142 795b88cb 2023-07-10 thomas if (error)
5143 795b88cb 2023-07-10 thomas goto done;
5144 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5145 795b88cb 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5146 795b88cb 2023-07-10 thomas p) == -1) {
5147 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
5148 795b88cb 2023-07-10 thomas free(p);
5149 795b88cb 2023-07-10 thomas goto done;
5150 795b88cb 2023-07-10 thomas }
5151 795b88cb 2023-07-10 thomas free(p);
5152 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5153 795b88cb 2023-07-10 thomas } else {
5154 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5155 795b88cb 2023-07-10 thomas if (error)
5156 795b88cb 2023-07-10 thomas goto done;
5157 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5158 795b88cb 2023-07-10 thomas }
5159 795b88cb 2023-07-10 thomas if (error)
5160 795b88cb 2023-07-10 thomas goto done;
5161 795b88cb 2023-07-10 thomas
5162 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
5163 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
5164 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, worktree ?
5165 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5166 795b88cb 2023-07-10 thomas if (error != NULL)
5167 795b88cb 2023-07-10 thomas goto done;
5168 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5169 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
5170 795b88cb 2023-07-10 thomas if (error != NULL)
5171 795b88cb 2023-07-10 thomas goto done;
5172 795b88cb 2023-07-10 thomas } else {
5173 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5174 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5175 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5176 795b88cb 2023-07-10 thomas NULL);
5177 795b88cb 2023-07-10 thomas if (error)
5178 795b88cb 2023-07-10 thomas goto done;
5179 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5180 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5181 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
5182 795b88cb 2023-07-10 thomas if (error)
5183 795b88cb 2023-07-10 thomas goto done;
5184 795b88cb 2023-07-10 thomas }
5185 795b88cb 2023-07-10 thomas
5186 795b88cb 2023-07-10 thomas if (worktree) {
5187 795b88cb 2023-07-10 thomas /* Release work tree lock. */
5188 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5189 795b88cb 2023-07-10 thomas worktree = NULL;
5190 795b88cb 2023-07-10 thomas }
5191 795b88cb 2023-07-10 thomas
5192 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5193 795b88cb 2023-07-10 thomas if (error)
5194 795b88cb 2023-07-10 thomas goto done;
5195 795b88cb 2023-07-10 thomas
5196 795b88cb 2023-07-10 thomas error = got_object_resolve_symlinks(&link_target, in_repo_path,
5197 795b88cb 2023-07-10 thomas commit, repo);
5198 795b88cb 2023-07-10 thomas if (error)
5199 795b88cb 2023-07-10 thomas goto done;
5200 795b88cb 2023-07-10 thomas
5201 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&obj_id, repo, commit,
5202 795b88cb 2023-07-10 thomas link_target ? link_target : in_repo_path);
5203 795b88cb 2023-07-10 thomas if (error)
5204 795b88cb 2023-07-10 thomas goto done;
5205 795b88cb 2023-07-10 thomas
5206 795b88cb 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, obj_id);
5207 795b88cb 2023-07-10 thomas if (error)
5208 795b88cb 2023-07-10 thomas goto done;
5209 795b88cb 2023-07-10 thomas
5210 795b88cb 2023-07-10 thomas if (obj_type != GOT_OBJ_TYPE_BLOB) {
5211 795b88cb 2023-07-10 thomas error = got_error_path(link_target ? link_target : in_repo_path,
5212 795b88cb 2023-07-10 thomas GOT_ERR_OBJ_TYPE);
5213 795b88cb 2023-07-10 thomas goto done;
5214 795b88cb 2023-07-10 thomas }
5215 795b88cb 2023-07-10 thomas
5216 795b88cb 2023-07-10 thomas error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5217 795b88cb 2023-07-10 thomas if (error)
5218 795b88cb 2023-07-10 thomas goto done;
5219 795b88cb 2023-07-10 thomas bca.f = got_opentemp();
5220 795b88cb 2023-07-10 thomas if (bca.f == NULL) {
5221 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5222 795b88cb 2023-07-10 thomas goto done;
5223 795b88cb 2023-07-10 thomas }
5224 795b88cb 2023-07-10 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5225 795b88cb 2023-07-10 thomas &bca.line_offsets, bca.f, blob);
5226 795b88cb 2023-07-10 thomas if (error || bca.nlines == 0)
5227 795b88cb 2023-07-10 thomas goto done;
5228 795b88cb 2023-07-10 thomas
5229 795b88cb 2023-07-10 thomas /* Don't include \n at EOF in the blame line count. */
5230 795b88cb 2023-07-10 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
5231 795b88cb 2023-07-10 thomas bca.nlines--;
5232 795b88cb 2023-07-10 thomas
5233 795b88cb 2023-07-10 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5234 795b88cb 2023-07-10 thomas if (bca.lines == NULL) {
5235 795b88cb 2023-07-10 thomas error = got_error_from_errno("calloc");
5236 795b88cb 2023-07-10 thomas goto done;
5237 795b88cb 2023-07-10 thomas }
5238 795b88cb 2023-07-10 thomas bca.lineno_cur = 1;
5239 795b88cb 2023-07-10 thomas bca.nlines_prec = 0;
5240 795b88cb 2023-07-10 thomas i = bca.nlines;
5241 795b88cb 2023-07-10 thomas while (i > 0) {
5242 795b88cb 2023-07-10 thomas i /= 10;
5243 795b88cb 2023-07-10 thomas bca.nlines_prec++;
5244 795b88cb 2023-07-10 thomas }
5245 795b88cb 2023-07-10 thomas bca.repo = repo;
5246 795b88cb 2023-07-10 thomas
5247 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
5248 795b88cb 2023-07-10 thomas if (fd2 == -1) {
5249 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5250 795b88cb 2023-07-10 thomas goto done;
5251 795b88cb 2023-07-10 thomas }
5252 795b88cb 2023-07-10 thomas fd3 = got_opentempfd();
5253 795b88cb 2023-07-10 thomas if (fd3 == -1) {
5254 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5255 795b88cb 2023-07-10 thomas goto done;
5256 795b88cb 2023-07-10 thomas }
5257 795b88cb 2023-07-10 thomas f1 = got_opentemp();
5258 795b88cb 2023-07-10 thomas if (f1 == NULL) {
5259 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5260 795b88cb 2023-07-10 thomas goto done;
5261 795b88cb 2023-07-10 thomas }
5262 795b88cb 2023-07-10 thomas f2 = got_opentemp();
5263 795b88cb 2023-07-10 thomas if (f2 == NULL) {
5264 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5265 795b88cb 2023-07-10 thomas goto done;
5266 795b88cb 2023-07-10 thomas }
5267 795b88cb 2023-07-10 thomas error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5268 795b88cb 2023-07-10 thomas repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5269 795b88cb 2023-07-10 thomas check_cancelled, NULL, fd2, fd3, f1, f2);
5270 795b88cb 2023-07-10 thomas done:
5271 795b88cb 2023-07-10 thomas free(in_repo_path);
5272 795b88cb 2023-07-10 thomas free(link_target);
5273 795b88cb 2023-07-10 thomas free(repo_path);
5274 795b88cb 2023-07-10 thomas free(cwd);
5275 795b88cb 2023-07-10 thomas free(commit_id);
5276 795b88cb 2023-07-10 thomas free(obj_id);
5277 795b88cb 2023-07-10 thomas if (commit)
5278 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
5279 795b88cb 2023-07-10 thomas
5280 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5281 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5282 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5283 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5284 795b88cb 2023-07-10 thomas if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5285 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5286 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
5287 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5288 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
5289 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5290 795b88cb 2023-07-10 thomas
5291 795b88cb 2023-07-10 thomas if (blob)
5292 795b88cb 2023-07-10 thomas got_object_blob_close(blob);
5293 795b88cb 2023-07-10 thomas if (worktree)
5294 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5295 795b88cb 2023-07-10 thomas if (repo) {
5296 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5297 795b88cb 2023-07-10 thomas if (error == NULL)
5298 795b88cb 2023-07-10 thomas error = close_err;
5299 795b88cb 2023-07-10 thomas }
5300 795b88cb 2023-07-10 thomas if (pack_fds) {
5301 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5302 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5303 795b88cb 2023-07-10 thomas if (error == NULL)
5304 795b88cb 2023-07-10 thomas error = pack_err;
5305 795b88cb 2023-07-10 thomas }
5306 795b88cb 2023-07-10 thomas if (bca.lines) {
5307 795b88cb 2023-07-10 thomas for (i = 0; i < bca.nlines; i++) {
5308 795b88cb 2023-07-10 thomas struct blame_line *bline = &bca.lines[i];
5309 795b88cb 2023-07-10 thomas free(bline->id_str);
5310 795b88cb 2023-07-10 thomas free(bline->committer);
5311 795b88cb 2023-07-10 thomas }
5312 795b88cb 2023-07-10 thomas free(bca.lines);
5313 795b88cb 2023-07-10 thomas }
5314 795b88cb 2023-07-10 thomas free(bca.line_offsets);
5315 795b88cb 2023-07-10 thomas if (bca.f && fclose(bca.f) == EOF && error == NULL)
5316 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5317 795b88cb 2023-07-10 thomas return error;
5318 795b88cb 2023-07-10 thomas }
5319 795b88cb 2023-07-10 thomas
5320 795b88cb 2023-07-10 thomas __dead static void
5321 795b88cb 2023-07-10 thomas usage_tree(void)
5322 795b88cb 2023-07-10 thomas {
5323 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5324 795b88cb 2023-07-10 thomas "[path]\n", getprogname());
5325 795b88cb 2023-07-10 thomas exit(1);
5326 795b88cb 2023-07-10 thomas }
5327 795b88cb 2023-07-10 thomas
5328 795b88cb 2023-07-10 thomas static const struct got_error *
5329 795b88cb 2023-07-10 thomas print_entry(struct got_tree_entry *te, const char *id, const char *path,
5330 795b88cb 2023-07-10 thomas const char *root_path, struct got_repository *repo)
5331 795b88cb 2023-07-10 thomas {
5332 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5333 795b88cb 2023-07-10 thomas int is_root_path = (strcmp(path, root_path) == 0);
5334 795b88cb 2023-07-10 thomas const char *modestr = "";
5335 795b88cb 2023-07-10 thomas mode_t mode = got_tree_entry_get_mode(te);
5336 795b88cb 2023-07-10 thomas char *link_target = NULL;
5337 795b88cb 2023-07-10 thomas
5338 795b88cb 2023-07-10 thomas path += strlen(root_path);
5339 795b88cb 2023-07-10 thomas while (path[0] == '/')
5340 795b88cb 2023-07-10 thomas path++;
5341 795b88cb 2023-07-10 thomas
5342 795b88cb 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te))
5343 795b88cb 2023-07-10 thomas modestr = "$";
5344 795b88cb 2023-07-10 thomas else if (S_ISLNK(mode)) {
5345 795b88cb 2023-07-10 thomas int i;
5346 795b88cb 2023-07-10 thomas
5347 795b88cb 2023-07-10 thomas err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5348 795b88cb 2023-07-10 thomas if (err)
5349 795b88cb 2023-07-10 thomas return err;
5350 795b88cb 2023-07-10 thomas for (i = 0; link_target[i] != '\0'; i++) {
5351 795b88cb 2023-07-10 thomas if (!isprint((unsigned char)link_target[i]))
5352 795b88cb 2023-07-10 thomas link_target[i] = '?';
5353 795b88cb 2023-07-10 thomas }
5354 795b88cb 2023-07-10 thomas
5355 795b88cb 2023-07-10 thomas modestr = "@";
5356 795b88cb 2023-07-10 thomas }
5357 795b88cb 2023-07-10 thomas else if (S_ISDIR(mode))
5358 795b88cb 2023-07-10 thomas modestr = "/";
5359 795b88cb 2023-07-10 thomas else if (mode & S_IXUSR)
5360 795b88cb 2023-07-10 thomas modestr = "*";
5361 795b88cb 2023-07-10 thomas
5362 795b88cb 2023-07-10 thomas printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5363 795b88cb 2023-07-10 thomas is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5364 795b88cb 2023-07-10 thomas link_target ? " -> ": "", link_target ? link_target : "");
5365 795b88cb 2023-07-10 thomas
5366 795b88cb 2023-07-10 thomas free(link_target);
5367 795b88cb 2023-07-10 thomas return NULL;
5368 795b88cb 2023-07-10 thomas }
5369 795b88cb 2023-07-10 thomas
5370 795b88cb 2023-07-10 thomas static const struct got_error *
5371 795b88cb 2023-07-10 thomas print_tree(const char *path, struct got_commit_object *commit,
5372 795b88cb 2023-07-10 thomas int show_ids, int recurse, const char *root_path,
5373 795b88cb 2023-07-10 thomas struct got_repository *repo)
5374 795b88cb 2023-07-10 thomas {
5375 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5376 795b88cb 2023-07-10 thomas struct got_object_id *tree_id = NULL;
5377 795b88cb 2023-07-10 thomas struct got_tree_object *tree = NULL;
5378 795b88cb 2023-07-10 thomas int nentries, i;
5379 795b88cb 2023-07-10 thomas
5380 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&tree_id, repo, commit, path);
5381 795b88cb 2023-07-10 thomas if (err)
5382 795b88cb 2023-07-10 thomas goto done;
5383 795b88cb 2023-07-10 thomas
5384 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, tree_id);
5385 795b88cb 2023-07-10 thomas if (err)
5386 795b88cb 2023-07-10 thomas goto done;
5387 795b88cb 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
5388 795b88cb 2023-07-10 thomas for (i = 0; i < nentries; i++) {
5389 795b88cb 2023-07-10 thomas struct got_tree_entry *te;
5390 795b88cb 2023-07-10 thomas char *id = NULL;
5391 795b88cb 2023-07-10 thomas
5392 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
5393 795b88cb 2023-07-10 thomas break;
5394 795b88cb 2023-07-10 thomas
5395 795b88cb 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
5396 795b88cb 2023-07-10 thomas if (show_ids) {
5397 795b88cb 2023-07-10 thomas char *id_str;
5398 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str,
5399 795b88cb 2023-07-10 thomas got_tree_entry_get_id(te));
5400 795b88cb 2023-07-10 thomas if (err)
5401 795b88cb 2023-07-10 thomas goto done;
5402 795b88cb 2023-07-10 thomas if (asprintf(&id, "%s ", id_str) == -1) {
5403 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5404 795b88cb 2023-07-10 thomas free(id_str);
5405 795b88cb 2023-07-10 thomas goto done;
5406 795b88cb 2023-07-10 thomas }
5407 795b88cb 2023-07-10 thomas free(id_str);
5408 795b88cb 2023-07-10 thomas }
5409 795b88cb 2023-07-10 thomas err = print_entry(te, id, path, root_path, repo);
5410 795b88cb 2023-07-10 thomas free(id);
5411 795b88cb 2023-07-10 thomas if (err)
5412 795b88cb 2023-07-10 thomas goto done;
5413 795b88cb 2023-07-10 thomas
5414 795b88cb 2023-07-10 thomas if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5415 795b88cb 2023-07-10 thomas char *child_path;
5416 795b88cb 2023-07-10 thomas if (asprintf(&child_path, "%s%s%s", path,
5417 795b88cb 2023-07-10 thomas path[0] == '/' && path[1] == '\0' ? "" : "/",
5418 795b88cb 2023-07-10 thomas got_tree_entry_get_name(te)) == -1) {
5419 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5420 795b88cb 2023-07-10 thomas goto done;
5421 795b88cb 2023-07-10 thomas }
5422 795b88cb 2023-07-10 thomas err = print_tree(child_path, commit, show_ids, 1,
5423 795b88cb 2023-07-10 thomas root_path, repo);
5424 795b88cb 2023-07-10 thomas free(child_path);
5425 795b88cb 2023-07-10 thomas if (err)
5426 795b88cb 2023-07-10 thomas goto done;
5427 795b88cb 2023-07-10 thomas }
5428 795b88cb 2023-07-10 thomas }
5429 795b88cb 2023-07-10 thomas done:
5430 795b88cb 2023-07-10 thomas if (tree)
5431 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
5432 795b88cb 2023-07-10 thomas free(tree_id);
5433 795b88cb 2023-07-10 thomas return err;
5434 795b88cb 2023-07-10 thomas }
5435 795b88cb 2023-07-10 thomas
5436 795b88cb 2023-07-10 thomas static const struct got_error *
5437 795b88cb 2023-07-10 thomas cmd_tree(int argc, char *argv[])
5438 795b88cb 2023-07-10 thomas {
5439 795b88cb 2023-07-10 thomas const struct got_error *error;
5440 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5441 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5442 795b88cb 2023-07-10 thomas const char *path, *refname = NULL;
5443 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5444 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5445 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5446 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
5447 795b88cb 2023-07-10 thomas int show_ids = 0, recurse = 0;
5448 795b88cb 2023-07-10 thomas int ch;
5449 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5450 795b88cb 2023-07-10 thomas
5451 795b88cb 2023-07-10 thomas #ifndef PROFILE
5452 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5453 795b88cb 2023-07-10 thomas NULL) == -1)
5454 795b88cb 2023-07-10 thomas err(1, "pledge");
5455 795b88cb 2023-07-10 thomas #endif
5456 795b88cb 2023-07-10 thomas
5457 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
5458 795b88cb 2023-07-10 thomas switch (ch) {
5459 795b88cb 2023-07-10 thomas case 'c':
5460 795b88cb 2023-07-10 thomas commit_id_str = optarg;
5461 795b88cb 2023-07-10 thomas break;
5462 795b88cb 2023-07-10 thomas case 'i':
5463 795b88cb 2023-07-10 thomas show_ids = 1;
5464 795b88cb 2023-07-10 thomas break;
5465 795b88cb 2023-07-10 thomas case 'R':
5466 795b88cb 2023-07-10 thomas recurse = 1;
5467 795b88cb 2023-07-10 thomas break;
5468 795b88cb 2023-07-10 thomas case 'r':
5469 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5470 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5471 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
5472 795b88cb 2023-07-10 thomas optarg);
5473 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5474 795b88cb 2023-07-10 thomas break;
5475 795b88cb 2023-07-10 thomas default:
5476 795b88cb 2023-07-10 thomas usage_tree();
5477 795b88cb 2023-07-10 thomas /* NOTREACHED */
5478 795b88cb 2023-07-10 thomas }
5479 795b88cb 2023-07-10 thomas }
5480 795b88cb 2023-07-10 thomas
5481 795b88cb 2023-07-10 thomas argc -= optind;
5482 795b88cb 2023-07-10 thomas argv += optind;
5483 795b88cb 2023-07-10 thomas
5484 795b88cb 2023-07-10 thomas if (argc == 1)
5485 795b88cb 2023-07-10 thomas path = argv[0];
5486 795b88cb 2023-07-10 thomas else if (argc > 1)
5487 795b88cb 2023-07-10 thomas usage_tree();
5488 795b88cb 2023-07-10 thomas else
5489 795b88cb 2023-07-10 thomas path = NULL;
5490 795b88cb 2023-07-10 thomas
5491 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5492 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5493 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5494 795b88cb 2023-07-10 thomas goto done;
5495 795b88cb 2023-07-10 thomas }
5496 795b88cb 2023-07-10 thomas
5497 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5498 795b88cb 2023-07-10 thomas if (error != NULL)
5499 795b88cb 2023-07-10 thomas goto done;
5500 795b88cb 2023-07-10 thomas
5501 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5502 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5503 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5504 795b88cb 2023-07-10 thomas goto done;
5505 795b88cb 2023-07-10 thomas else
5506 795b88cb 2023-07-10 thomas error = NULL;
5507 795b88cb 2023-07-10 thomas if (worktree) {
5508 795b88cb 2023-07-10 thomas repo_path =
5509 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5510 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5511 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5512 795b88cb 2023-07-10 thomas if (error)
5513 795b88cb 2023-07-10 thomas goto done;
5514 795b88cb 2023-07-10 thomas } else {
5515 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
5516 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5517 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5518 795b88cb 2023-07-10 thomas goto done;
5519 795b88cb 2023-07-10 thomas }
5520 795b88cb 2023-07-10 thomas }
5521 795b88cb 2023-07-10 thomas }
5522 795b88cb 2023-07-10 thomas
5523 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5524 795b88cb 2023-07-10 thomas if (error != NULL)
5525 795b88cb 2023-07-10 thomas goto done;
5526 795b88cb 2023-07-10 thomas
5527 795b88cb 2023-07-10 thomas if (worktree) {
5528 795b88cb 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5529 795b88cb 2023-07-10 thomas char *p;
5530 795b88cb 2023-07-10 thomas
5531 795b88cb 2023-07-10 thomas if (path == NULL || got_path_is_root_dir(path))
5532 795b88cb 2023-07-10 thomas path = "";
5533 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5534 795b88cb 2023-07-10 thomas if (error)
5535 795b88cb 2023-07-10 thomas goto done;
5536 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5537 795b88cb 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5538 795b88cb 2023-07-10 thomas p) == -1) {
5539 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
5540 795b88cb 2023-07-10 thomas free(p);
5541 795b88cb 2023-07-10 thomas goto done;
5542 795b88cb 2023-07-10 thomas }
5543 795b88cb 2023-07-10 thomas free(p);
5544 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5545 795b88cb 2023-07-10 thomas if (error)
5546 795b88cb 2023-07-10 thomas goto done;
5547 795b88cb 2023-07-10 thomas } else {
5548 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5549 795b88cb 2023-07-10 thomas if (error)
5550 795b88cb 2023-07-10 thomas goto done;
5551 795b88cb 2023-07-10 thomas if (path == NULL)
5552 795b88cb 2023-07-10 thomas path = "/";
5553 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5554 795b88cb 2023-07-10 thomas if (error != NULL)
5555 795b88cb 2023-07-10 thomas goto done;
5556 795b88cb 2023-07-10 thomas }
5557 795b88cb 2023-07-10 thomas
5558 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
5559 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
5560 795b88cb 2023-07-10 thomas if (worktree)
5561 795b88cb 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
5562 795b88cb 2023-07-10 thomas else
5563 795b88cb 2023-07-10 thomas refname = GOT_REF_HEAD;
5564 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, refname, 0);
5565 795b88cb 2023-07-10 thomas if (error != NULL)
5566 795b88cb 2023-07-10 thomas goto done;
5567 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5568 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
5569 795b88cb 2023-07-10 thomas if (error != NULL)
5570 795b88cb 2023-07-10 thomas goto done;
5571 795b88cb 2023-07-10 thomas } else {
5572 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5573 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5574 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5575 795b88cb 2023-07-10 thomas NULL);
5576 795b88cb 2023-07-10 thomas if (error)
5577 795b88cb 2023-07-10 thomas goto done;
5578 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5579 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5580 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
5581 795b88cb 2023-07-10 thomas if (error)
5582 795b88cb 2023-07-10 thomas goto done;
5583 795b88cb 2023-07-10 thomas }
5584 795b88cb 2023-07-10 thomas
5585 795b88cb 2023-07-10 thomas if (worktree) {
5586 795b88cb 2023-07-10 thomas /* Release work tree lock. */
5587 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5588 795b88cb 2023-07-10 thomas worktree = NULL;
5589 795b88cb 2023-07-10 thomas }
5590 795b88cb 2023-07-10 thomas
5591 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5592 795b88cb 2023-07-10 thomas if (error)
5593 795b88cb 2023-07-10 thomas goto done;
5594 795b88cb 2023-07-10 thomas
5595 795b88cb 2023-07-10 thomas error = print_tree(in_repo_path, commit, show_ids, recurse,
5596 795b88cb 2023-07-10 thomas in_repo_path, repo);
5597 795b88cb 2023-07-10 thomas done:
5598 795b88cb 2023-07-10 thomas free(in_repo_path);
5599 795b88cb 2023-07-10 thomas free(repo_path);
5600 795b88cb 2023-07-10 thomas free(cwd);
5601 795b88cb 2023-07-10 thomas free(commit_id);
5602 795b88cb 2023-07-10 thomas if (commit)
5603 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
5604 795b88cb 2023-07-10 thomas if (worktree)
5605 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5606 795b88cb 2023-07-10 thomas if (repo) {
5607 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5608 795b88cb 2023-07-10 thomas if (error == NULL)
5609 795b88cb 2023-07-10 thomas error = close_err;
5610 795b88cb 2023-07-10 thomas }
5611 795b88cb 2023-07-10 thomas if (pack_fds) {
5612 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5613 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5614 795b88cb 2023-07-10 thomas if (error == NULL)
5615 795b88cb 2023-07-10 thomas error = pack_err;
5616 795b88cb 2023-07-10 thomas }
5617 795b88cb 2023-07-10 thomas return error;
5618 795b88cb 2023-07-10 thomas }
5619 795b88cb 2023-07-10 thomas
5620 795b88cb 2023-07-10 thomas __dead static void
5621 795b88cb 2023-07-10 thomas usage_status(void)
5622 795b88cb 2023-07-10 thomas {
5623 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
5624 795b88cb 2023-07-10 thomas "[-s status-codes] [path ...]\n", getprogname());
5625 795b88cb 2023-07-10 thomas exit(1);
5626 795b88cb 2023-07-10 thomas }
5627 795b88cb 2023-07-10 thomas
5628 795b88cb 2023-07-10 thomas struct got_status_arg {
5629 795b88cb 2023-07-10 thomas char *status_codes;
5630 795b88cb 2023-07-10 thomas int suppress;
5631 795b88cb 2023-07-10 thomas };
5632 795b88cb 2023-07-10 thomas
5633 795b88cb 2023-07-10 thomas static const struct got_error *
5634 795b88cb 2023-07-10 thomas print_status(void *arg, unsigned char status, unsigned char staged_status,
5635 795b88cb 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
5636 795b88cb 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5637 795b88cb 2023-07-10 thomas int dirfd, const char *de_name)
5638 795b88cb 2023-07-10 thomas {
5639 795b88cb 2023-07-10 thomas struct got_status_arg *st = arg;
5640 795b88cb 2023-07-10 thomas
5641 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
5642 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
5643 795b88cb 2023-07-10 thomas if (st != NULL && st->status_codes) {
5644 795b88cb 2023-07-10 thomas size_t ncodes = strlen(st->status_codes);
5645 795b88cb 2023-07-10 thomas int i, j = 0;
5646 795b88cb 2023-07-10 thomas
5647 795b88cb 2023-07-10 thomas for (i = 0; i < ncodes ; i++) {
5648 795b88cb 2023-07-10 thomas if (st->suppress) {
5649 795b88cb 2023-07-10 thomas if (status == st->status_codes[i] ||
5650 795b88cb 2023-07-10 thomas staged_status == st->status_codes[i]) {
5651 795b88cb 2023-07-10 thomas j++;
5652 795b88cb 2023-07-10 thomas continue;
5653 795b88cb 2023-07-10 thomas }
5654 795b88cb 2023-07-10 thomas } else {
5655 795b88cb 2023-07-10 thomas if (status == st->status_codes[i] ||
5656 795b88cb 2023-07-10 thomas staged_status == st->status_codes[i])
5657 795b88cb 2023-07-10 thomas break;
5658 795b88cb 2023-07-10 thomas }
5659 795b88cb 2023-07-10 thomas }
5660 795b88cb 2023-07-10 thomas
5661 795b88cb 2023-07-10 thomas if (st->suppress && j == 0)
5662 795b88cb 2023-07-10 thomas goto print;
5663 795b88cb 2023-07-10 thomas
5664 795b88cb 2023-07-10 thomas if (i == ncodes)
5665 795b88cb 2023-07-10 thomas return NULL;
5666 795b88cb 2023-07-10 thomas }
5667 795b88cb 2023-07-10 thomas print:
5668 795b88cb 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
5669 795b88cb 2023-07-10 thomas return NULL;
5670 795b88cb 2023-07-10 thomas }
5671 795b88cb 2023-07-10 thomas
5672 795b88cb 2023-07-10 thomas static const struct got_error *
5673 795b88cb 2023-07-10 thomas cmd_status(int argc, char *argv[])
5674 795b88cb 2023-07-10 thomas {
5675 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
5676 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5677 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5678 795b88cb 2023-07-10 thomas struct got_status_arg st;
5679 795b88cb 2023-07-10 thomas char *cwd = NULL;
5680 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
5681 795b88cb 2023-07-10 thomas int ch, i, no_ignores = 0;
5682 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5683 795b88cb 2023-07-10 thomas
5684 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
5685 795b88cb 2023-07-10 thomas
5686 795b88cb 2023-07-10 thomas memset(&st, 0, sizeof(st));
5687 795b88cb 2023-07-10 thomas st.status_codes = NULL;
5688 795b88cb 2023-07-10 thomas st.suppress = 0;
5689 795b88cb 2023-07-10 thomas
5690 795b88cb 2023-07-10 thomas #ifndef PROFILE
5691 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5692 795b88cb 2023-07-10 thomas NULL) == -1)
5693 795b88cb 2023-07-10 thomas err(1, "pledge");
5694 795b88cb 2023-07-10 thomas #endif
5695 795b88cb 2023-07-10 thomas
5696 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
5697 795b88cb 2023-07-10 thomas switch (ch) {
5698 795b88cb 2023-07-10 thomas case 'I':
5699 795b88cb 2023-07-10 thomas no_ignores = 1;
5700 795b88cb 2023-07-10 thomas break;
5701 795b88cb 2023-07-10 thomas case 'S':
5702 795b88cb 2023-07-10 thomas if (st.status_codes != NULL && st.suppress == 0)
5703 795b88cb 2023-07-10 thomas option_conflict('S', 's');
5704 795b88cb 2023-07-10 thomas st.suppress = 1;
5705 795b88cb 2023-07-10 thomas /* fallthrough */
5706 795b88cb 2023-07-10 thomas case 's':
5707 795b88cb 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
5708 795b88cb 2023-07-10 thomas switch (optarg[i]) {
5709 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
5710 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
5711 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
5712 795b88cb 2023-07-10 thomas case GOT_STATUS_CONFLICT:
5713 795b88cb 2023-07-10 thomas case GOT_STATUS_MISSING:
5714 795b88cb 2023-07-10 thomas case GOT_STATUS_OBSTRUCTED:
5715 795b88cb 2023-07-10 thomas case GOT_STATUS_UNVERSIONED:
5716 795b88cb 2023-07-10 thomas case GOT_STATUS_MODE_CHANGE:
5717 795b88cb 2023-07-10 thomas case GOT_STATUS_NONEXISTENT:
5718 795b88cb 2023-07-10 thomas break;
5719 795b88cb 2023-07-10 thomas default:
5720 795b88cb 2023-07-10 thomas errx(1, "invalid status code '%c'",
5721 795b88cb 2023-07-10 thomas optarg[i]);
5722 795b88cb 2023-07-10 thomas }
5723 795b88cb 2023-07-10 thomas }
5724 795b88cb 2023-07-10 thomas if (ch == 's' && st.suppress)
5725 795b88cb 2023-07-10 thomas option_conflict('s', 'S');
5726 795b88cb 2023-07-10 thomas st.status_codes = optarg;
5727 795b88cb 2023-07-10 thomas break;
5728 795b88cb 2023-07-10 thomas default:
5729 795b88cb 2023-07-10 thomas usage_status();
5730 795b88cb 2023-07-10 thomas /* NOTREACHED */
5731 795b88cb 2023-07-10 thomas }
5732 795b88cb 2023-07-10 thomas }
5733 795b88cb 2023-07-10 thomas
5734 795b88cb 2023-07-10 thomas argc -= optind;
5735 795b88cb 2023-07-10 thomas argv += optind;
5736 795b88cb 2023-07-10 thomas
5737 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5738 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5739 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5740 795b88cb 2023-07-10 thomas goto done;
5741 795b88cb 2023-07-10 thomas }
5742 795b88cb 2023-07-10 thomas
5743 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5744 795b88cb 2023-07-10 thomas if (error != NULL)
5745 795b88cb 2023-07-10 thomas goto done;
5746 795b88cb 2023-07-10 thomas
5747 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5748 795b88cb 2023-07-10 thomas if (error) {
5749 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
5750 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "status", cwd);
5751 795b88cb 2023-07-10 thomas goto done;
5752 795b88cb 2023-07-10 thomas }
5753 795b88cb 2023-07-10 thomas
5754 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5755 795b88cb 2023-07-10 thomas NULL, pack_fds);
5756 795b88cb 2023-07-10 thomas if (error != NULL)
5757 795b88cb 2023-07-10 thomas goto done;
5758 795b88cb 2023-07-10 thomas
5759 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
5760 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
5761 795b88cb 2023-07-10 thomas if (error)
5762 795b88cb 2023-07-10 thomas goto done;
5763 795b88cb 2023-07-10 thomas
5764 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5765 795b88cb 2023-07-10 thomas if (error)
5766 795b88cb 2023-07-10 thomas goto done;
5767 795b88cb 2023-07-10 thomas
5768 795b88cb 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, no_ignores,
5769 795b88cb 2023-07-10 thomas print_status, &st, check_cancelled, NULL);
5770 795b88cb 2023-07-10 thomas done:
5771 795b88cb 2023-07-10 thomas if (pack_fds) {
5772 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5773 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5774 795b88cb 2023-07-10 thomas if (error == NULL)
5775 795b88cb 2023-07-10 thomas error = pack_err;
5776 795b88cb 2023-07-10 thomas }
5777 795b88cb 2023-07-10 thomas if (repo) {
5778 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5779 795b88cb 2023-07-10 thomas if (error == NULL)
5780 795b88cb 2023-07-10 thomas error = close_err;
5781 795b88cb 2023-07-10 thomas }
5782 795b88cb 2023-07-10 thomas
5783 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5784 795b88cb 2023-07-10 thomas free(cwd);
5785 795b88cb 2023-07-10 thomas return error;
5786 795b88cb 2023-07-10 thomas }
5787 795b88cb 2023-07-10 thomas
5788 795b88cb 2023-07-10 thomas __dead static void
5789 795b88cb 2023-07-10 thomas usage_tag(void)
5790 795b88cb 2023-07-10 thomas {
5791 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
5792 795b88cb 2023-07-10 thomas "[-r repository-path] [-s signer-id] name\n", getprogname());
5793 795b88cb 2023-07-10 thomas exit(1);
5794 795b88cb 2023-07-10 thomas }
5795 795b88cb 2023-07-10 thomas
5796 795b88cb 2023-07-10 thomas #if 0
5797 795b88cb 2023-07-10 thomas static const struct got_error *
5798 795b88cb 2023-07-10 thomas sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5799 795b88cb 2023-07-10 thomas {
5800 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5801 795b88cb 2023-07-10 thomas struct got_reflist_entry *re, *se, *new;
5802 795b88cb 2023-07-10 thomas struct got_object_id *re_id, *se_id;
5803 795b88cb 2023-07-10 thomas struct got_tag_object *re_tag, *se_tag;
5804 795b88cb 2023-07-10 thomas time_t re_time, se_time;
5805 795b88cb 2023-07-10 thomas
5806 795b88cb 2023-07-10 thomas STAILQ_FOREACH(re, tags, entry) {
5807 795b88cb 2023-07-10 thomas se = STAILQ_FIRST(sorted);
5808 795b88cb 2023-07-10 thomas if (se == NULL) {
5809 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
5810 795b88cb 2023-07-10 thomas if (err)
5811 795b88cb 2023-07-10 thomas return err;
5812 795b88cb 2023-07-10 thomas STAILQ_INSERT_HEAD(sorted, new, entry);
5813 795b88cb 2023-07-10 thomas continue;
5814 795b88cb 2023-07-10 thomas } else {
5815 795b88cb 2023-07-10 thomas err = got_ref_resolve(&re_id, repo, re->ref);
5816 795b88cb 2023-07-10 thomas if (err)
5817 795b88cb 2023-07-10 thomas break;
5818 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&re_tag, repo, re_id);
5819 795b88cb 2023-07-10 thomas free(re_id);
5820 795b88cb 2023-07-10 thomas if (err)
5821 795b88cb 2023-07-10 thomas break;
5822 795b88cb 2023-07-10 thomas re_time = got_object_tag_get_tagger_time(re_tag);
5823 795b88cb 2023-07-10 thomas got_object_tag_close(re_tag);
5824 795b88cb 2023-07-10 thomas }
5825 795b88cb 2023-07-10 thomas
5826 795b88cb 2023-07-10 thomas while (se) {
5827 795b88cb 2023-07-10 thomas err = got_ref_resolve(&se_id, repo, re->ref);
5828 795b88cb 2023-07-10 thomas if (err)
5829 795b88cb 2023-07-10 thomas break;
5830 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&se_tag, repo, se_id);
5831 795b88cb 2023-07-10 thomas free(se_id);
5832 795b88cb 2023-07-10 thomas if (err)
5833 795b88cb 2023-07-10 thomas break;
5834 795b88cb 2023-07-10 thomas se_time = got_object_tag_get_tagger_time(se_tag);
5835 795b88cb 2023-07-10 thomas got_object_tag_close(se_tag);
5836 795b88cb 2023-07-10 thomas
5837 795b88cb 2023-07-10 thomas if (se_time > re_time) {
5838 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
5839 795b88cb 2023-07-10 thomas if (err)
5840 795b88cb 2023-07-10 thomas return err;
5841 795b88cb 2023-07-10 thomas STAILQ_INSERT_AFTER(sorted, se, new, entry);
5842 795b88cb 2023-07-10 thomas break;
5843 795b88cb 2023-07-10 thomas }
5844 795b88cb 2023-07-10 thomas se = STAILQ_NEXT(se, entry);
5845 795b88cb 2023-07-10 thomas continue;
5846 795b88cb 2023-07-10 thomas }
5847 795b88cb 2023-07-10 thomas }
5848 795b88cb 2023-07-10 thomas done:
5849 795b88cb 2023-07-10 thomas return err;
5850 795b88cb 2023-07-10 thomas }
5851 795b88cb 2023-07-10 thomas #endif
5852 795b88cb 2023-07-10 thomas
5853 795b88cb 2023-07-10 thomas static const struct got_error *
5854 795b88cb 2023-07-10 thomas get_tag_refname(char **refname, const char *tag_name)
5855 795b88cb 2023-07-10 thomas {
5856 795b88cb 2023-07-10 thomas const struct got_error *err;
5857 795b88cb 2023-07-10 thomas
5858 795b88cb 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0) {
5859 795b88cb 2023-07-10 thomas *refname = strdup(tag_name);
5860 795b88cb 2023-07-10 thomas if (*refname == NULL)
5861 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
5862 795b88cb 2023-07-10 thomas } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
5863 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5864 795b88cb 2023-07-10 thomas *refname = NULL;
5865 795b88cb 2023-07-10 thomas return err;
5866 795b88cb 2023-07-10 thomas }
5867 795b88cb 2023-07-10 thomas
5868 795b88cb 2023-07-10 thomas return NULL;
5869 795b88cb 2023-07-10 thomas }
5870 795b88cb 2023-07-10 thomas
5871 795b88cb 2023-07-10 thomas static const struct got_error *
5872 795b88cb 2023-07-10 thomas list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
5873 795b88cb 2023-07-10 thomas const char *allowed_signers, const char *revoked_signers, int verbosity)
5874 795b88cb 2023-07-10 thomas {
5875 795b88cb 2023-07-10 thomas static const struct got_error *err = NULL;
5876 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5877 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
5878 795b88cb 2023-07-10 thomas char *wanted_refname = NULL;
5879 795b88cb 2023-07-10 thomas int bad_sigs = 0;
5880 795b88cb 2023-07-10 thomas
5881 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5882 795b88cb 2023-07-10 thomas
5883 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5884 795b88cb 2023-07-10 thomas if (err)
5885 795b88cb 2023-07-10 thomas return err;
5886 795b88cb 2023-07-10 thomas
5887 795b88cb 2023-07-10 thomas if (tag_name) {
5888 795b88cb 2023-07-10 thomas struct got_reference *ref;
5889 795b88cb 2023-07-10 thomas err = get_tag_refname(&wanted_refname, tag_name);
5890 795b88cb 2023-07-10 thomas if (err)
5891 795b88cb 2023-07-10 thomas goto done;
5892 795b88cb 2023-07-10 thomas /* Wanted tag reference should exist. */
5893 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, wanted_refname, 0);
5894 795b88cb 2023-07-10 thomas if (err)
5895 795b88cb 2023-07-10 thomas goto done;
5896 795b88cb 2023-07-10 thomas got_ref_close(ref);
5897 795b88cb 2023-07-10 thomas }
5898 795b88cb 2023-07-10 thomas
5899 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
5900 795b88cb 2023-07-10 thomas const char *refname;
5901 795b88cb 2023-07-10 thomas char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5902 795b88cb 2023-07-10 thomas char datebuf[26];
5903 795b88cb 2023-07-10 thomas const char *tagger, *ssh_sig = NULL;
5904 795b88cb 2023-07-10 thomas char *sig_msg = NULL;
5905 795b88cb 2023-07-10 thomas time_t tagger_time;
5906 795b88cb 2023-07-10 thomas struct got_object_id *id;
5907 795b88cb 2023-07-10 thomas struct got_tag_object *tag;
5908 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5909 795b88cb 2023-07-10 thomas
5910 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
5911 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/tags/", 10) != 0 ||
5912 795b88cb 2023-07-10 thomas (wanted_refname && strcmp(refname, wanted_refname) != 0))
5913 795b88cb 2023-07-10 thomas continue;
5914 795b88cb 2023-07-10 thomas refname += 10;
5915 795b88cb 2023-07-10 thomas refstr = got_ref_to_str(re->ref);
5916 795b88cb 2023-07-10 thomas if (refstr == NULL) {
5917 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_ref_to_str");
5918 795b88cb 2023-07-10 thomas break;
5919 795b88cb 2023-07-10 thomas }
5920 795b88cb 2023-07-10 thomas
5921 795b88cb 2023-07-10 thomas err = got_ref_resolve(&id, repo, re->ref);
5922 795b88cb 2023-07-10 thomas if (err)
5923 795b88cb 2023-07-10 thomas break;
5924 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
5925 795b88cb 2023-07-10 thomas if (err) {
5926 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
5927 795b88cb 2023-07-10 thomas free(id);
5928 795b88cb 2023-07-10 thomas break;
5929 795b88cb 2023-07-10 thomas }
5930 795b88cb 2023-07-10 thomas /* "lightweight" tag */
5931 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
5932 795b88cb 2023-07-10 thomas if (err) {
5933 795b88cb 2023-07-10 thomas free(id);
5934 795b88cb 2023-07-10 thomas break;
5935 795b88cb 2023-07-10 thomas }
5936 795b88cb 2023-07-10 thomas tagger = got_object_commit_get_committer(commit);
5937 795b88cb 2023-07-10 thomas tagger_time =
5938 795b88cb 2023-07-10 thomas got_object_commit_get_committer_time(commit);
5939 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
5940 795b88cb 2023-07-10 thomas free(id);
5941 795b88cb 2023-07-10 thomas if (err)
5942 795b88cb 2023-07-10 thomas break;
5943 795b88cb 2023-07-10 thomas } else {
5944 795b88cb 2023-07-10 thomas free(id);
5945 795b88cb 2023-07-10 thomas tagger = got_object_tag_get_tagger(tag);
5946 795b88cb 2023-07-10 thomas tagger_time = got_object_tag_get_tagger_time(tag);
5947 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str,
5948 795b88cb 2023-07-10 thomas got_object_tag_get_object_id(tag));
5949 795b88cb 2023-07-10 thomas if (err)
5950 795b88cb 2023-07-10 thomas break;
5951 795b88cb 2023-07-10 thomas }
5952 795b88cb 2023-07-10 thomas
5953 795b88cb 2023-07-10 thomas if (tag && verify_tags) {
5954 795b88cb 2023-07-10 thomas ssh_sig = got_sigs_get_tagmsg_ssh_signature(
5955 795b88cb 2023-07-10 thomas got_object_tag_get_message(tag));
5956 795b88cb 2023-07-10 thomas if (ssh_sig && allowed_signers == NULL) {
5957 795b88cb 2023-07-10 thomas err = got_error_msg(
5958 795b88cb 2023-07-10 thomas GOT_ERR_VERIFY_TAG_SIGNATURE,
5959 795b88cb 2023-07-10 thomas "SSH signature verification requires "
5960 795b88cb 2023-07-10 thomas "setting allowed_signers in "
5961 795b88cb 2023-07-10 thomas "got.conf(5)");
5962 795b88cb 2023-07-10 thomas break;
5963 795b88cb 2023-07-10 thomas }
5964 795b88cb 2023-07-10 thomas }
5965 795b88cb 2023-07-10 thomas
5966 795b88cb 2023-07-10 thomas printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5967 795b88cb 2023-07-10 thomas free(refstr);
5968 795b88cb 2023-07-10 thomas printf("from: %s\n", tagger);
5969 795b88cb 2023-07-10 thomas datestr = get_datestr(&tagger_time, datebuf);
5970 795b88cb 2023-07-10 thomas if (datestr)
5971 795b88cb 2023-07-10 thomas printf("date: %s UTC\n", datestr);
5972 795b88cb 2023-07-10 thomas if (commit)
5973 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5974 795b88cb 2023-07-10 thomas else {
5975 795b88cb 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
5976 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
5977 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5978 795b88cb 2023-07-10 thomas id_str);
5979 795b88cb 2023-07-10 thomas break;
5980 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
5981 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5982 795b88cb 2023-07-10 thomas id_str);
5983 795b88cb 2023-07-10 thomas break;
5984 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
5985 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5986 795b88cb 2023-07-10 thomas id_str);
5987 795b88cb 2023-07-10 thomas break;
5988 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
5989 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5990 795b88cb 2023-07-10 thomas id_str);
5991 795b88cb 2023-07-10 thomas break;
5992 795b88cb 2023-07-10 thomas default:
5993 795b88cb 2023-07-10 thomas break;
5994 795b88cb 2023-07-10 thomas }
5995 795b88cb 2023-07-10 thomas }
5996 795b88cb 2023-07-10 thomas free(id_str);
5997 795b88cb 2023-07-10 thomas
5998 795b88cb 2023-07-10 thomas if (ssh_sig) {
5999 795b88cb 2023-07-10 thomas err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
6000 795b88cb 2023-07-10 thomas allowed_signers, revoked_signers, verbosity);
6001 795b88cb 2023-07-10 thomas if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
6002 795b88cb 2023-07-10 thomas bad_sigs = 1;
6003 795b88cb 2023-07-10 thomas else if (err)
6004 795b88cb 2023-07-10 thomas break;
6005 795b88cb 2023-07-10 thomas printf("signature: %s", sig_msg);
6006 795b88cb 2023-07-10 thomas free(sig_msg);
6007 795b88cb 2023-07-10 thomas sig_msg = NULL;
6008 795b88cb 2023-07-10 thomas }
6009 795b88cb 2023-07-10 thomas
6010 795b88cb 2023-07-10 thomas if (commit) {
6011 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&tagmsg0, commit);
6012 795b88cb 2023-07-10 thomas if (err)
6013 795b88cb 2023-07-10 thomas break;
6014 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
6015 795b88cb 2023-07-10 thomas } else {
6016 795b88cb 2023-07-10 thomas tagmsg0 = strdup(got_object_tag_get_message(tag));
6017 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
6018 795b88cb 2023-07-10 thomas if (tagmsg0 == NULL) {
6019 795b88cb 2023-07-10 thomas err = got_error_from_errno("strdup");
6020 795b88cb 2023-07-10 thomas break;
6021 795b88cb 2023-07-10 thomas }
6022 795b88cb 2023-07-10 thomas }
6023 795b88cb 2023-07-10 thomas
6024 795b88cb 2023-07-10 thomas tagmsg = tagmsg0;
6025 795b88cb 2023-07-10 thomas do {
6026 795b88cb 2023-07-10 thomas line = strsep(&tagmsg, "\n");
6027 795b88cb 2023-07-10 thomas if (line)
6028 795b88cb 2023-07-10 thomas printf(" %s\n", line);
6029 795b88cb 2023-07-10 thomas } while (line);
6030 795b88cb 2023-07-10 thomas free(tagmsg0);
6031 795b88cb 2023-07-10 thomas }
6032 795b88cb 2023-07-10 thomas done:
6033 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6034 795b88cb 2023-07-10 thomas free(wanted_refname);
6035 795b88cb 2023-07-10 thomas
6036 795b88cb 2023-07-10 thomas if (err == NULL && bad_sigs)
6037 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
6038 795b88cb 2023-07-10 thomas return err;
6039 795b88cb 2023-07-10 thomas }
6040 795b88cb 2023-07-10 thomas
6041 795b88cb 2023-07-10 thomas static const struct got_error *
6042 795b88cb 2023-07-10 thomas get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6043 795b88cb 2023-07-10 thomas const char *tag_name, const char *repo_path)
6044 795b88cb 2023-07-10 thomas {
6045 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6046 795b88cb 2023-07-10 thomas char *template = NULL, *initial_content = NULL;
6047 795b88cb 2023-07-10 thomas char *editor = NULL;
6048 795b88cb 2023-07-10 thomas int initial_content_len;
6049 795b88cb 2023-07-10 thomas int fd = -1;
6050 795b88cb 2023-07-10 thomas
6051 795b88cb 2023-07-10 thomas if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6052 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
6053 795b88cb 2023-07-10 thomas goto done;
6054 795b88cb 2023-07-10 thomas }
6055 795b88cb 2023-07-10 thomas
6056 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
6057 795b88cb 2023-07-10 thomas "\n# tagging commit %s as %s\n",
6058 795b88cb 2023-07-10 thomas commit_id_str, tag_name);
6059 795b88cb 2023-07-10 thomas if (initial_content_len == -1) {
6060 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
6061 795b88cb 2023-07-10 thomas goto done;
6062 795b88cb 2023-07-10 thomas }
6063 795b88cb 2023-07-10 thomas
6064 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
6065 795b88cb 2023-07-10 thomas if (err)
6066 795b88cb 2023-07-10 thomas goto done;
6067 795b88cb 2023-07-10 thomas
6068 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
6069 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", *tagmsg_path);
6070 795b88cb 2023-07-10 thomas goto done;
6071 795b88cb 2023-07-10 thomas }
6072 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
6073 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
6074 795b88cb 2023-07-10 thomas goto done;
6075 795b88cb 2023-07-10 thomas }
6076 795b88cb 2023-07-10 thomas fd = -1;
6077 795b88cb 2023-07-10 thomas
6078 795b88cb 2023-07-10 thomas err = get_editor(&editor);
6079 795b88cb 2023-07-10 thomas if (err)
6080 795b88cb 2023-07-10 thomas goto done;
6081 795b88cb 2023-07-10 thomas err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6082 795b88cb 2023-07-10 thomas initial_content_len, 1);
6083 795b88cb 2023-07-10 thomas done:
6084 795b88cb 2023-07-10 thomas free(initial_content);
6085 795b88cb 2023-07-10 thomas free(template);
6086 795b88cb 2023-07-10 thomas free(editor);
6087 795b88cb 2023-07-10 thomas
6088 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6089 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
6090 795b88cb 2023-07-10 thomas
6091 795b88cb 2023-07-10 thomas if (err) {
6092 795b88cb 2023-07-10 thomas free(*tagmsg);
6093 795b88cb 2023-07-10 thomas *tagmsg = NULL;
6094 795b88cb 2023-07-10 thomas }
6095 795b88cb 2023-07-10 thomas return err;
6096 795b88cb 2023-07-10 thomas }
6097 795b88cb 2023-07-10 thomas
6098 795b88cb 2023-07-10 thomas static const struct got_error *
6099 795b88cb 2023-07-10 thomas add_tag(struct got_repository *repo, const char *tagger,
6100 795b88cb 2023-07-10 thomas const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
6101 795b88cb 2023-07-10 thomas const char *signer_id, int verbosity)
6102 795b88cb 2023-07-10 thomas {
6103 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6104 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL, *tag_id = NULL;
6105 795b88cb 2023-07-10 thomas char *label = NULL, *commit_id_str = NULL;
6106 795b88cb 2023-07-10 thomas struct got_reference *ref = NULL;
6107 795b88cb 2023-07-10 thomas char *refname = NULL, *tagmsg = NULL;
6108 795b88cb 2023-07-10 thomas char *tagmsg_path = NULL, *tag_id_str = NULL;
6109 795b88cb 2023-07-10 thomas int preserve_tagmsg = 0;
6110 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
6111 795b88cb 2023-07-10 thomas
6112 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
6113 795b88cb 2023-07-10 thomas
6114 795b88cb 2023-07-10 thomas /*
6115 795b88cb 2023-07-10 thomas * Don't let the user create a tag name with a leading '-'.
6116 795b88cb 2023-07-10 thomas * While technically a valid reference name, this case is usually
6117 795b88cb 2023-07-10 thomas * an unintended typo.
6118 795b88cb 2023-07-10 thomas */
6119 795b88cb 2023-07-10 thomas if (tag_name[0] == '-')
6120 795b88cb 2023-07-10 thomas return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6121 795b88cb 2023-07-10 thomas
6122 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6123 795b88cb 2023-07-10 thomas if (err)
6124 795b88cb 2023-07-10 thomas goto done;
6125 795b88cb 2023-07-10 thomas
6126 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6127 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
6128 795b88cb 2023-07-10 thomas if (err)
6129 795b88cb 2023-07-10 thomas goto done;
6130 795b88cb 2023-07-10 thomas
6131 795b88cb 2023-07-10 thomas err = got_object_id_str(&commit_id_str, commit_id);
6132 795b88cb 2023-07-10 thomas if (err)
6133 795b88cb 2023-07-10 thomas goto done;
6134 795b88cb 2023-07-10 thomas
6135 795b88cb 2023-07-10 thomas err = get_tag_refname(&refname, tag_name);
6136 795b88cb 2023-07-10 thomas if (err)
6137 795b88cb 2023-07-10 thomas goto done;
6138 795b88cb 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0)
6139 795b88cb 2023-07-10 thomas tag_name += 10;
6140 795b88cb 2023-07-10 thomas
6141 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
6142 795b88cb 2023-07-10 thomas if (err == NULL) {
6143 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_TAG_EXISTS);
6144 795b88cb 2023-07-10 thomas goto done;
6145 795b88cb 2023-07-10 thomas } else if (err->code != GOT_ERR_NOT_REF)
6146 795b88cb 2023-07-10 thomas goto done;
6147 795b88cb 2023-07-10 thomas
6148 795b88cb 2023-07-10 thomas if (tagmsg_arg == NULL) {
6149 795b88cb 2023-07-10 thomas err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6150 795b88cb 2023-07-10 thomas tag_name, got_repo_get_path(repo));
6151 795b88cb 2023-07-10 thomas if (err) {
6152 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6153 795b88cb 2023-07-10 thomas tagmsg_path != NULL)
6154 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6155 795b88cb 2023-07-10 thomas goto done;
6156 795b88cb 2023-07-10 thomas }
6157 795b88cb 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
6158 795b88cb 2023-07-10 thomas err = got_sigs_apply_unveil();
6159 795b88cb 2023-07-10 thomas if (err)
6160 795b88cb 2023-07-10 thomas goto done;
6161 795b88cb 2023-07-10 thomas err = apply_unveil(got_repo_get_path(repo), 0, NULL);
6162 795b88cb 2023-07-10 thomas if (err)
6163 795b88cb 2023-07-10 thomas goto done;
6164 795b88cb 2023-07-10 thomas }
6165 795b88cb 2023-07-10 thomas
6166 795b88cb 2023-07-10 thomas err = got_object_tag_create(&tag_id, tag_name, commit_id,
6167 795b88cb 2023-07-10 thomas tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
6168 795b88cb 2023-07-10 thomas verbosity);
6169 795b88cb 2023-07-10 thomas if (err) {
6170 795b88cb 2023-07-10 thomas if (tagmsg_path)
6171 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6172 795b88cb 2023-07-10 thomas goto done;
6173 795b88cb 2023-07-10 thomas }
6174 795b88cb 2023-07-10 thomas
6175 795b88cb 2023-07-10 thomas err = got_ref_alloc(&ref, refname, tag_id);
6176 795b88cb 2023-07-10 thomas if (err) {
6177 795b88cb 2023-07-10 thomas if (tagmsg_path)
6178 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6179 795b88cb 2023-07-10 thomas goto done;
6180 795b88cb 2023-07-10 thomas }
6181 795b88cb 2023-07-10 thomas
6182 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
6183 795b88cb 2023-07-10 thomas if (err) {
6184 795b88cb 2023-07-10 thomas if (tagmsg_path)
6185 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6186 795b88cb 2023-07-10 thomas goto done;
6187 795b88cb 2023-07-10 thomas }
6188 795b88cb 2023-07-10 thomas
6189 795b88cb 2023-07-10 thomas err = got_object_id_str(&tag_id_str, tag_id);
6190 795b88cb 2023-07-10 thomas if (err) {
6191 795b88cb 2023-07-10 thomas if (tagmsg_path)
6192 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6193 795b88cb 2023-07-10 thomas goto done;
6194 795b88cb 2023-07-10 thomas }
6195 795b88cb 2023-07-10 thomas printf("Created tag %s\n", tag_id_str);
6196 795b88cb 2023-07-10 thomas done:
6197 795b88cb 2023-07-10 thomas if (preserve_tagmsg) {
6198 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: tag message preserved in %s\n",
6199 795b88cb 2023-07-10 thomas getprogname(), tagmsg_path);
6200 795b88cb 2023-07-10 thomas } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6201 795b88cb 2023-07-10 thomas err = got_error_from_errno2("unlink", tagmsg_path);
6202 795b88cb 2023-07-10 thomas free(tag_id_str);
6203 795b88cb 2023-07-10 thomas if (ref)
6204 795b88cb 2023-07-10 thomas got_ref_close(ref);
6205 795b88cb 2023-07-10 thomas free(commit_id);
6206 795b88cb 2023-07-10 thomas free(commit_id_str);
6207 795b88cb 2023-07-10 thomas free(refname);
6208 795b88cb 2023-07-10 thomas free(tagmsg);
6209 795b88cb 2023-07-10 thomas free(tagmsg_path);
6210 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6211 795b88cb 2023-07-10 thomas return err;
6212 795b88cb 2023-07-10 thomas }
6213 795b88cb 2023-07-10 thomas
6214 795b88cb 2023-07-10 thomas static const struct got_error *
6215 795b88cb 2023-07-10 thomas cmd_tag(int argc, char *argv[])
6216 795b88cb 2023-07-10 thomas {
6217 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6218 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6219 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6220 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6221 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *tagger = NULL;
6222 795b88cb 2023-07-10 thomas char *allowed_signers = NULL, *revoked_signers = NULL;
6223 795b88cb 2023-07-10 thomas const char *signer_id = NULL;
6224 795b88cb 2023-07-10 thomas const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
6225 795b88cb 2023-07-10 thomas int ch, do_list = 0, verify_tags = 0, verbosity = 0;
6226 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6227 795b88cb 2023-07-10 thomas
6228 795b88cb 2023-07-10 thomas #ifndef PROFILE
6229 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6230 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
6231 795b88cb 2023-07-10 thomas err(1, "pledge");
6232 795b88cb 2023-07-10 thomas #endif
6233 795b88cb 2023-07-10 thomas
6234 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
6235 795b88cb 2023-07-10 thomas switch (ch) {
6236 795b88cb 2023-07-10 thomas case 'c':
6237 795b88cb 2023-07-10 thomas commit_id_arg = optarg;
6238 795b88cb 2023-07-10 thomas break;
6239 795b88cb 2023-07-10 thomas case 'l':
6240 795b88cb 2023-07-10 thomas do_list = 1;
6241 795b88cb 2023-07-10 thomas break;
6242 795b88cb 2023-07-10 thomas case 'm':
6243 795b88cb 2023-07-10 thomas tagmsg = optarg;
6244 795b88cb 2023-07-10 thomas break;
6245 795b88cb 2023-07-10 thomas case 'r':
6246 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
6247 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6248 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
6249 795b88cb 2023-07-10 thomas optarg);
6250 795b88cb 2023-07-10 thomas goto done;
6251 795b88cb 2023-07-10 thomas }
6252 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
6253 795b88cb 2023-07-10 thomas break;
6254 795b88cb 2023-07-10 thomas case 's':
6255 795b88cb 2023-07-10 thomas signer_id = optarg;
6256 795b88cb 2023-07-10 thomas break;
6257 795b88cb 2023-07-10 thomas case 'V':
6258 795b88cb 2023-07-10 thomas verify_tags = 1;
6259 795b88cb 2023-07-10 thomas break;
6260 795b88cb 2023-07-10 thomas case 'v':
6261 795b88cb 2023-07-10 thomas if (verbosity < 0)
6262 795b88cb 2023-07-10 thomas verbosity = 0;
6263 795b88cb 2023-07-10 thomas else if (verbosity < 3)
6264 795b88cb 2023-07-10 thomas verbosity++;
6265 795b88cb 2023-07-10 thomas break;
6266 795b88cb 2023-07-10 thomas default:
6267 795b88cb 2023-07-10 thomas usage_tag();
6268 795b88cb 2023-07-10 thomas /* NOTREACHED */
6269 795b88cb 2023-07-10 thomas }
6270 795b88cb 2023-07-10 thomas }
6271 795b88cb 2023-07-10 thomas
6272 795b88cb 2023-07-10 thomas argc -= optind;
6273 795b88cb 2023-07-10 thomas argv += optind;
6274 795b88cb 2023-07-10 thomas
6275 795b88cb 2023-07-10 thomas if (do_list || verify_tags) {
6276 795b88cb 2023-07-10 thomas if (commit_id_arg != NULL)
6277 795b88cb 2023-07-10 thomas errx(1,
6278 795b88cb 2023-07-10 thomas "-c option can only be used when creating a tag");
6279 795b88cb 2023-07-10 thomas if (tagmsg) {
6280 795b88cb 2023-07-10 thomas if (do_list)
6281 795b88cb 2023-07-10 thomas option_conflict('l', 'm');
6282 795b88cb 2023-07-10 thomas else
6283 795b88cb 2023-07-10 thomas option_conflict('V', 'm');
6284 795b88cb 2023-07-10 thomas }
6285 795b88cb 2023-07-10 thomas if (signer_id) {
6286 795b88cb 2023-07-10 thomas if (do_list)
6287 795b88cb 2023-07-10 thomas option_conflict('l', 's');
6288 795b88cb 2023-07-10 thomas else
6289 795b88cb 2023-07-10 thomas option_conflict('V', 's');
6290 795b88cb 2023-07-10 thomas }
6291 795b88cb 2023-07-10 thomas if (argc > 1)
6292 795b88cb 2023-07-10 thomas usage_tag();
6293 795b88cb 2023-07-10 thomas } else if (argc != 1)
6294 795b88cb 2023-07-10 thomas usage_tag();
6295 795b88cb 2023-07-10 thomas
6296 795b88cb 2023-07-10 thomas if (argc == 1)
6297 795b88cb 2023-07-10 thomas tag_name = argv[0];
6298 795b88cb 2023-07-10 thomas
6299 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6300 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6301 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6302 795b88cb 2023-07-10 thomas goto done;
6303 795b88cb 2023-07-10 thomas }
6304 795b88cb 2023-07-10 thomas
6305 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6306 795b88cb 2023-07-10 thomas if (error != NULL)
6307 795b88cb 2023-07-10 thomas goto done;
6308 795b88cb 2023-07-10 thomas
6309 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6310 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6311 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
6312 795b88cb 2023-07-10 thomas goto done;
6313 795b88cb 2023-07-10 thomas else
6314 795b88cb 2023-07-10 thomas error = NULL;
6315 795b88cb 2023-07-10 thomas if (worktree) {
6316 795b88cb 2023-07-10 thomas repo_path =
6317 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
6318 795b88cb 2023-07-10 thomas if (repo_path == NULL)
6319 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
6320 795b88cb 2023-07-10 thomas if (error)
6321 795b88cb 2023-07-10 thomas goto done;
6322 795b88cb 2023-07-10 thomas } else {
6323 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
6324 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6325 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
6326 795b88cb 2023-07-10 thomas goto done;
6327 795b88cb 2023-07-10 thomas }
6328 795b88cb 2023-07-10 thomas }
6329 795b88cb 2023-07-10 thomas }
6330 795b88cb 2023-07-10 thomas
6331 795b88cb 2023-07-10 thomas if (do_list || verify_tags) {
6332 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6333 795b88cb 2023-07-10 thomas if (error != NULL)
6334 795b88cb 2023-07-10 thomas goto done;
6335 795b88cb 2023-07-10 thomas error = get_allowed_signers(&allowed_signers, repo, worktree);
6336 795b88cb 2023-07-10 thomas if (error)
6337 795b88cb 2023-07-10 thomas goto done;
6338 795b88cb 2023-07-10 thomas error = get_revoked_signers(&revoked_signers, repo, worktree);
6339 795b88cb 2023-07-10 thomas if (error)
6340 795b88cb 2023-07-10 thomas goto done;
6341 795b88cb 2023-07-10 thomas if (worktree) {
6342 795b88cb 2023-07-10 thomas /* Release work tree lock. */
6343 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6344 795b88cb 2023-07-10 thomas worktree = NULL;
6345 795b88cb 2023-07-10 thomas }
6346 795b88cb 2023-07-10 thomas
6347 795b88cb 2023-07-10 thomas /*
6348 795b88cb 2023-07-10 thomas * Remove "cpath" promise unless needed for signature tmpfile
6349 795b88cb 2023-07-10 thomas * creation.
6350 795b88cb 2023-07-10 thomas */
6351 795b88cb 2023-07-10 thomas if (verify_tags)
6352 795b88cb 2023-07-10 thomas got_sigs_apply_unveil();
6353 795b88cb 2023-07-10 thomas else {
6354 795b88cb 2023-07-10 thomas #ifndef PROFILE
6355 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath flock proc exec sendfd "
6356 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
6357 795b88cb 2023-07-10 thomas err(1, "pledge");
6358 795b88cb 2023-07-10 thomas #endif
6359 795b88cb 2023-07-10 thomas }
6360 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6361 795b88cb 2023-07-10 thomas if (error)
6362 795b88cb 2023-07-10 thomas goto done;
6363 795b88cb 2023-07-10 thomas error = list_tags(repo, tag_name, verify_tags, allowed_signers,
6364 795b88cb 2023-07-10 thomas revoked_signers, verbosity);
6365 795b88cb 2023-07-10 thomas } else {
6366 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
6367 795b88cb 2023-07-10 thomas if (error)
6368 795b88cb 2023-07-10 thomas goto done;
6369 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path,
6370 795b88cb 2023-07-10 thomas pack_fds);
6371 795b88cb 2023-07-10 thomas if (error != NULL)
6372 795b88cb 2023-07-10 thomas goto done;
6373 795b88cb 2023-07-10 thomas
6374 795b88cb 2023-07-10 thomas error = get_author(&tagger, repo, worktree);
6375 795b88cb 2023-07-10 thomas if (error)
6376 795b88cb 2023-07-10 thomas goto done;
6377 795b88cb 2023-07-10 thomas if (signer_id == NULL)
6378 795b88cb 2023-07-10 thomas signer_id = get_signer_id(repo, worktree);
6379 795b88cb 2023-07-10 thomas
6380 795b88cb 2023-07-10 thomas if (tagmsg) {
6381 795b88cb 2023-07-10 thomas if (signer_id) {
6382 795b88cb 2023-07-10 thomas error = got_sigs_apply_unveil();
6383 795b88cb 2023-07-10 thomas if (error)
6384 795b88cb 2023-07-10 thomas goto done;
6385 795b88cb 2023-07-10 thomas }
6386 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6387 795b88cb 2023-07-10 thomas if (error)
6388 795b88cb 2023-07-10 thomas goto done;
6389 795b88cb 2023-07-10 thomas }
6390 795b88cb 2023-07-10 thomas
6391 795b88cb 2023-07-10 thomas if (commit_id_arg == NULL) {
6392 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
6393 795b88cb 2023-07-10 thomas struct got_object_id *commit_id;
6394 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
6395 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
6396 795b88cb 2023-07-10 thomas : GOT_REF_HEAD, 0);
6397 795b88cb 2023-07-10 thomas if (error)
6398 795b88cb 2023-07-10 thomas goto done;
6399 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
6400 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
6401 795b88cb 2023-07-10 thomas if (error)
6402 795b88cb 2023-07-10 thomas goto done;
6403 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
6404 795b88cb 2023-07-10 thomas free(commit_id);
6405 795b88cb 2023-07-10 thomas if (error)
6406 795b88cb 2023-07-10 thomas goto done;
6407 795b88cb 2023-07-10 thomas }
6408 795b88cb 2023-07-10 thomas
6409 795b88cb 2023-07-10 thomas if (worktree) {
6410 795b88cb 2023-07-10 thomas /* Release work tree lock. */
6411 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6412 795b88cb 2023-07-10 thomas worktree = NULL;
6413 795b88cb 2023-07-10 thomas }
6414 795b88cb 2023-07-10 thomas
6415 795b88cb 2023-07-10 thomas error = add_tag(repo, tagger, tag_name,
6416 795b88cb 2023-07-10 thomas commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
6417 795b88cb 2023-07-10 thomas signer_id, verbosity);
6418 795b88cb 2023-07-10 thomas }
6419 795b88cb 2023-07-10 thomas done:
6420 795b88cb 2023-07-10 thomas if (repo) {
6421 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6422 795b88cb 2023-07-10 thomas if (error == NULL)
6423 795b88cb 2023-07-10 thomas error = close_err;
6424 795b88cb 2023-07-10 thomas }
6425 795b88cb 2023-07-10 thomas if (worktree)
6426 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6427 795b88cb 2023-07-10 thomas if (pack_fds) {
6428 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6429 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6430 795b88cb 2023-07-10 thomas if (error == NULL)
6431 795b88cb 2023-07-10 thomas error = pack_err;
6432 795b88cb 2023-07-10 thomas }
6433 795b88cb 2023-07-10 thomas free(cwd);
6434 795b88cb 2023-07-10 thomas free(repo_path);
6435 795b88cb 2023-07-10 thomas free(gitconfig_path);
6436 795b88cb 2023-07-10 thomas free(commit_id_str);
6437 795b88cb 2023-07-10 thomas free(tagger);
6438 795b88cb 2023-07-10 thomas free(allowed_signers);
6439 795b88cb 2023-07-10 thomas free(revoked_signers);
6440 795b88cb 2023-07-10 thomas return error;
6441 795b88cb 2023-07-10 thomas }
6442 795b88cb 2023-07-10 thomas
6443 795b88cb 2023-07-10 thomas __dead static void
6444 795b88cb 2023-07-10 thomas usage_add(void)
6445 795b88cb 2023-07-10 thomas {
6446 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
6447 795b88cb 2023-07-10 thomas exit(1);
6448 795b88cb 2023-07-10 thomas }
6449 795b88cb 2023-07-10 thomas
6450 795b88cb 2023-07-10 thomas static const struct got_error *
6451 795b88cb 2023-07-10 thomas add_progress(void *arg, unsigned char status, const char *path)
6452 795b88cb 2023-07-10 thomas {
6453 795b88cb 2023-07-10 thomas while (path[0] == '/')
6454 795b88cb 2023-07-10 thomas path++;
6455 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
6456 795b88cb 2023-07-10 thomas return NULL;
6457 795b88cb 2023-07-10 thomas }
6458 795b88cb 2023-07-10 thomas
6459 795b88cb 2023-07-10 thomas static const struct got_error *
6460 795b88cb 2023-07-10 thomas cmd_add(int argc, char *argv[])
6461 795b88cb 2023-07-10 thomas {
6462 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6463 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6464 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6465 795b88cb 2023-07-10 thomas char *cwd = NULL;
6466 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
6467 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
6468 795b88cb 2023-07-10 thomas int ch, can_recurse = 0, no_ignores = 0;
6469 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6470 795b88cb 2023-07-10 thomas
6471 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
6472 795b88cb 2023-07-10 thomas
6473 795b88cb 2023-07-10 thomas #ifndef PROFILE
6474 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6475 795b88cb 2023-07-10 thomas NULL) == -1)
6476 795b88cb 2023-07-10 thomas err(1, "pledge");
6477 795b88cb 2023-07-10 thomas #endif
6478 795b88cb 2023-07-10 thomas
6479 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "IR")) != -1) {
6480 795b88cb 2023-07-10 thomas switch (ch) {
6481 795b88cb 2023-07-10 thomas case 'I':
6482 795b88cb 2023-07-10 thomas no_ignores = 1;
6483 795b88cb 2023-07-10 thomas break;
6484 795b88cb 2023-07-10 thomas case 'R':
6485 795b88cb 2023-07-10 thomas can_recurse = 1;
6486 795b88cb 2023-07-10 thomas break;
6487 795b88cb 2023-07-10 thomas default:
6488 795b88cb 2023-07-10 thomas usage_add();
6489 795b88cb 2023-07-10 thomas /* NOTREACHED */
6490 795b88cb 2023-07-10 thomas }
6491 795b88cb 2023-07-10 thomas }
6492 795b88cb 2023-07-10 thomas
6493 795b88cb 2023-07-10 thomas argc -= optind;
6494 795b88cb 2023-07-10 thomas argv += optind;
6495 795b88cb 2023-07-10 thomas
6496 795b88cb 2023-07-10 thomas if (argc < 1)
6497 795b88cb 2023-07-10 thomas usage_add();
6498 795b88cb 2023-07-10 thomas
6499 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6500 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6501 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6502 795b88cb 2023-07-10 thomas goto done;
6503 795b88cb 2023-07-10 thomas }
6504 795b88cb 2023-07-10 thomas
6505 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6506 795b88cb 2023-07-10 thomas if (error != NULL)
6507 795b88cb 2023-07-10 thomas goto done;
6508 795b88cb 2023-07-10 thomas
6509 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6510 795b88cb 2023-07-10 thomas if (error) {
6511 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6512 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "add", cwd);
6513 795b88cb 2023-07-10 thomas goto done;
6514 795b88cb 2023-07-10 thomas }
6515 795b88cb 2023-07-10 thomas
6516 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6517 795b88cb 2023-07-10 thomas NULL, pack_fds);
6518 795b88cb 2023-07-10 thomas if (error != NULL)
6519 795b88cb 2023-07-10 thomas goto done;
6520 795b88cb 2023-07-10 thomas
6521 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6522 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6523 795b88cb 2023-07-10 thomas if (error)
6524 795b88cb 2023-07-10 thomas goto done;
6525 795b88cb 2023-07-10 thomas
6526 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6527 795b88cb 2023-07-10 thomas if (error)
6528 795b88cb 2023-07-10 thomas goto done;
6529 795b88cb 2023-07-10 thomas
6530 795b88cb 2023-07-10 thomas if (!can_recurse) {
6531 795b88cb 2023-07-10 thomas char *ondisk_path;
6532 795b88cb 2023-07-10 thomas struct stat sb;
6533 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
6534 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
6535 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
6536 795b88cb 2023-07-10 thomas pe->path) == -1) {
6537 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
6538 795b88cb 2023-07-10 thomas goto done;
6539 795b88cb 2023-07-10 thomas }
6540 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
6541 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
6542 795b88cb 2023-07-10 thomas free(ondisk_path);
6543 795b88cb 2023-07-10 thomas continue;
6544 795b88cb 2023-07-10 thomas }
6545 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
6546 795b88cb 2023-07-10 thomas ondisk_path);
6547 795b88cb 2023-07-10 thomas free(ondisk_path);
6548 795b88cb 2023-07-10 thomas goto done;
6549 795b88cb 2023-07-10 thomas }
6550 795b88cb 2023-07-10 thomas free(ondisk_path);
6551 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
6552 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
6553 795b88cb 2023-07-10 thomas "adding directories requires -R option");
6554 795b88cb 2023-07-10 thomas goto done;
6555 795b88cb 2023-07-10 thomas }
6556 795b88cb 2023-07-10 thomas }
6557 795b88cb 2023-07-10 thomas }
6558 795b88cb 2023-07-10 thomas
6559 795b88cb 2023-07-10 thomas error = got_worktree_schedule_add(worktree, &paths, add_progress,
6560 795b88cb 2023-07-10 thomas NULL, repo, no_ignores);
6561 795b88cb 2023-07-10 thomas done:
6562 795b88cb 2023-07-10 thomas if (repo) {
6563 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6564 795b88cb 2023-07-10 thomas if (error == NULL)
6565 795b88cb 2023-07-10 thomas error = close_err;
6566 795b88cb 2023-07-10 thomas }
6567 795b88cb 2023-07-10 thomas if (worktree)
6568 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6569 795b88cb 2023-07-10 thomas if (pack_fds) {
6570 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6571 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6572 795b88cb 2023-07-10 thomas if (error == NULL)
6573 795b88cb 2023-07-10 thomas error = pack_err;
6574 795b88cb 2023-07-10 thomas }
6575 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6576 795b88cb 2023-07-10 thomas free(cwd);
6577 795b88cb 2023-07-10 thomas return error;
6578 795b88cb 2023-07-10 thomas }
6579 795b88cb 2023-07-10 thomas
6580 795b88cb 2023-07-10 thomas __dead static void
6581 795b88cb 2023-07-10 thomas usage_remove(void)
6582 795b88cb 2023-07-10 thomas {
6583 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
6584 795b88cb 2023-07-10 thomas getprogname());
6585 795b88cb 2023-07-10 thomas exit(1);
6586 795b88cb 2023-07-10 thomas }
6587 795b88cb 2023-07-10 thomas
6588 795b88cb 2023-07-10 thomas static const struct got_error *
6589 795b88cb 2023-07-10 thomas print_remove_status(void *arg, unsigned char status,
6590 795b88cb 2023-07-10 thomas unsigned char staged_status, const char *path)
6591 795b88cb 2023-07-10 thomas {
6592 795b88cb 2023-07-10 thomas while (path[0] == '/')
6593 795b88cb 2023-07-10 thomas path++;
6594 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
6595 795b88cb 2023-07-10 thomas return NULL;
6596 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
6597 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
6598 795b88cb 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
6599 795b88cb 2023-07-10 thomas return NULL;
6600 795b88cb 2023-07-10 thomas }
6601 795b88cb 2023-07-10 thomas
6602 795b88cb 2023-07-10 thomas static const struct got_error *
6603 795b88cb 2023-07-10 thomas cmd_remove(int argc, char *argv[])
6604 795b88cb 2023-07-10 thomas {
6605 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6606 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6607 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6608 795b88cb 2023-07-10 thomas const char *status_codes = NULL;
6609 795b88cb 2023-07-10 thomas char *cwd = NULL;
6610 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
6611 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
6612 795b88cb 2023-07-10 thomas int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6613 795b88cb 2023-07-10 thomas int ignore_missing_paths = 0;
6614 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6615 795b88cb 2023-07-10 thomas
6616 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
6617 795b88cb 2023-07-10 thomas
6618 795b88cb 2023-07-10 thomas #ifndef PROFILE
6619 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6620 795b88cb 2023-07-10 thomas NULL) == -1)
6621 795b88cb 2023-07-10 thomas err(1, "pledge");
6622 795b88cb 2023-07-10 thomas #endif
6623 795b88cb 2023-07-10 thomas
6624 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6625 795b88cb 2023-07-10 thomas switch (ch) {
6626 795b88cb 2023-07-10 thomas case 'f':
6627 795b88cb 2023-07-10 thomas delete_local_mods = 1;
6628 795b88cb 2023-07-10 thomas ignore_missing_paths = 1;
6629 795b88cb 2023-07-10 thomas break;
6630 795b88cb 2023-07-10 thomas case 'k':
6631 795b88cb 2023-07-10 thomas keep_on_disk = 1;
6632 795b88cb 2023-07-10 thomas break;
6633 795b88cb 2023-07-10 thomas case 'R':
6634 795b88cb 2023-07-10 thomas can_recurse = 1;
6635 795b88cb 2023-07-10 thomas break;
6636 795b88cb 2023-07-10 thomas case 's':
6637 795b88cb 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
6638 795b88cb 2023-07-10 thomas switch (optarg[i]) {
6639 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
6640 795b88cb 2023-07-10 thomas delete_local_mods = 1;
6641 795b88cb 2023-07-10 thomas break;
6642 795b88cb 2023-07-10 thomas case GOT_STATUS_MISSING:
6643 795b88cb 2023-07-10 thomas ignore_missing_paths = 1;
6644 795b88cb 2023-07-10 thomas break;
6645 795b88cb 2023-07-10 thomas default:
6646 795b88cb 2023-07-10 thomas errx(1, "invalid status code '%c'",
6647 795b88cb 2023-07-10 thomas optarg[i]);
6648 795b88cb 2023-07-10 thomas }
6649 795b88cb 2023-07-10 thomas }
6650 795b88cb 2023-07-10 thomas status_codes = optarg;
6651 795b88cb 2023-07-10 thomas break;
6652 795b88cb 2023-07-10 thomas default:
6653 795b88cb 2023-07-10 thomas usage_remove();
6654 795b88cb 2023-07-10 thomas /* NOTREACHED */
6655 795b88cb 2023-07-10 thomas }
6656 795b88cb 2023-07-10 thomas }
6657 795b88cb 2023-07-10 thomas
6658 795b88cb 2023-07-10 thomas argc -= optind;
6659 795b88cb 2023-07-10 thomas argv += optind;
6660 795b88cb 2023-07-10 thomas
6661 795b88cb 2023-07-10 thomas if (argc < 1)
6662 795b88cb 2023-07-10 thomas usage_remove();
6663 795b88cb 2023-07-10 thomas
6664 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6665 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6666 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6667 795b88cb 2023-07-10 thomas goto done;
6668 795b88cb 2023-07-10 thomas }
6669 795b88cb 2023-07-10 thomas
6670 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6671 795b88cb 2023-07-10 thomas if (error != NULL)
6672 795b88cb 2023-07-10 thomas goto done;
6673 795b88cb 2023-07-10 thomas
6674 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6675 795b88cb 2023-07-10 thomas if (error) {
6676 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6677 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "remove", cwd);
6678 795b88cb 2023-07-10 thomas goto done;
6679 795b88cb 2023-07-10 thomas }
6680 795b88cb 2023-07-10 thomas
6681 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6682 795b88cb 2023-07-10 thomas NULL, pack_fds);
6683 795b88cb 2023-07-10 thomas if (error)
6684 795b88cb 2023-07-10 thomas goto done;
6685 795b88cb 2023-07-10 thomas
6686 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6687 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6688 795b88cb 2023-07-10 thomas if (error)
6689 795b88cb 2023-07-10 thomas goto done;
6690 795b88cb 2023-07-10 thomas
6691 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6692 795b88cb 2023-07-10 thomas if (error)
6693 795b88cb 2023-07-10 thomas goto done;
6694 795b88cb 2023-07-10 thomas
6695 795b88cb 2023-07-10 thomas if (!can_recurse) {
6696 795b88cb 2023-07-10 thomas char *ondisk_path;
6697 795b88cb 2023-07-10 thomas struct stat sb;
6698 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
6699 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
6700 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
6701 795b88cb 2023-07-10 thomas pe->path) == -1) {
6702 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
6703 795b88cb 2023-07-10 thomas goto done;
6704 795b88cb 2023-07-10 thomas }
6705 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
6706 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
6707 795b88cb 2023-07-10 thomas free(ondisk_path);
6708 795b88cb 2023-07-10 thomas continue;
6709 795b88cb 2023-07-10 thomas }
6710 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
6711 795b88cb 2023-07-10 thomas ondisk_path);
6712 795b88cb 2023-07-10 thomas free(ondisk_path);
6713 795b88cb 2023-07-10 thomas goto done;
6714 795b88cb 2023-07-10 thomas }
6715 795b88cb 2023-07-10 thomas free(ondisk_path);
6716 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
6717 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
6718 795b88cb 2023-07-10 thomas "removing directories requires -R option");
6719 795b88cb 2023-07-10 thomas goto done;
6720 795b88cb 2023-07-10 thomas }
6721 795b88cb 2023-07-10 thomas }
6722 795b88cb 2023-07-10 thomas }
6723 795b88cb 2023-07-10 thomas
6724 795b88cb 2023-07-10 thomas error = got_worktree_schedule_delete(worktree, &paths,
6725 795b88cb 2023-07-10 thomas delete_local_mods, status_codes, print_remove_status, NULL,
6726 795b88cb 2023-07-10 thomas repo, keep_on_disk, ignore_missing_paths);
6727 795b88cb 2023-07-10 thomas done:
6728 795b88cb 2023-07-10 thomas if (repo) {
6729 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6730 795b88cb 2023-07-10 thomas if (error == NULL)
6731 795b88cb 2023-07-10 thomas error = close_err;
6732 795b88cb 2023-07-10 thomas }
6733 795b88cb 2023-07-10 thomas if (worktree)
6734 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6735 795b88cb 2023-07-10 thomas if (pack_fds) {
6736 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6737 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6738 795b88cb 2023-07-10 thomas if (error == NULL)
6739 795b88cb 2023-07-10 thomas error = pack_err;
6740 795b88cb 2023-07-10 thomas }
6741 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6742 795b88cb 2023-07-10 thomas free(cwd);
6743 795b88cb 2023-07-10 thomas return error;
6744 795b88cb 2023-07-10 thomas }
6745 795b88cb 2023-07-10 thomas
6746 795b88cb 2023-07-10 thomas __dead static void
6747 795b88cb 2023-07-10 thomas usage_patch(void)
6748 795b88cb 2023-07-10 thomas {
6749 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
6750 795b88cb 2023-07-10 thomas "[patchfile]\n", getprogname());
6751 795b88cb 2023-07-10 thomas exit(1);
6752 795b88cb 2023-07-10 thomas }
6753 795b88cb 2023-07-10 thomas
6754 795b88cb 2023-07-10 thomas static const struct got_error *
6755 795b88cb 2023-07-10 thomas patch_from_stdin(int *patchfd)
6756 795b88cb 2023-07-10 thomas {
6757 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6758 795b88cb 2023-07-10 thomas ssize_t r;
6759 795b88cb 2023-07-10 thomas char buf[BUFSIZ];
6760 795b88cb 2023-07-10 thomas sig_t sighup, sigint, sigquit;
6761 795b88cb 2023-07-10 thomas
6762 795b88cb 2023-07-10 thomas *patchfd = got_opentempfd();
6763 795b88cb 2023-07-10 thomas if (*patchfd == -1)
6764 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
6765 795b88cb 2023-07-10 thomas
6766 795b88cb 2023-07-10 thomas sighup = signal(SIGHUP, SIG_DFL);
6767 795b88cb 2023-07-10 thomas sigint = signal(SIGINT, SIG_DFL);
6768 795b88cb 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_DFL);
6769 795b88cb 2023-07-10 thomas
6770 795b88cb 2023-07-10 thomas for (;;) {
6771 795b88cb 2023-07-10 thomas r = read(0, buf, sizeof(buf));
6772 795b88cb 2023-07-10 thomas if (r == -1) {
6773 795b88cb 2023-07-10 thomas err = got_error_from_errno("read");
6774 795b88cb 2023-07-10 thomas break;
6775 795b88cb 2023-07-10 thomas }
6776 795b88cb 2023-07-10 thomas if (r == 0)
6777 795b88cb 2023-07-10 thomas break;
6778 795b88cb 2023-07-10 thomas if (write(*patchfd, buf, r) == -1) {
6779 795b88cb 2023-07-10 thomas err = got_error_from_errno("write");
6780 795b88cb 2023-07-10 thomas break;
6781 795b88cb 2023-07-10 thomas }
6782 795b88cb 2023-07-10 thomas }
6783 795b88cb 2023-07-10 thomas
6784 795b88cb 2023-07-10 thomas signal(SIGHUP, sighup);
6785 795b88cb 2023-07-10 thomas signal(SIGINT, sigint);
6786 795b88cb 2023-07-10 thomas signal(SIGQUIT, sigquit);
6787 795b88cb 2023-07-10 thomas
6788 795b88cb 2023-07-10 thomas if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
6789 795b88cb 2023-07-10 thomas err = got_error_from_errno("lseek");
6790 795b88cb 2023-07-10 thomas
6791 795b88cb 2023-07-10 thomas if (err != NULL) {
6792 795b88cb 2023-07-10 thomas close(*patchfd);
6793 795b88cb 2023-07-10 thomas *patchfd = -1;
6794 795b88cb 2023-07-10 thomas }
6795 795b88cb 2023-07-10 thomas
6796 795b88cb 2023-07-10 thomas return err;
6797 795b88cb 2023-07-10 thomas }
6798 795b88cb 2023-07-10 thomas
6799 795b88cb 2023-07-10 thomas struct got_patch_progress_arg {
6800 795b88cb 2023-07-10 thomas int did_something;
6801 795b88cb 2023-07-10 thomas int conflicts;
6802 795b88cb 2023-07-10 thomas int rejects;
6803 795b88cb 2023-07-10 thomas };
6804 795b88cb 2023-07-10 thomas
6805 795b88cb 2023-07-10 thomas static const struct got_error *
6806 795b88cb 2023-07-10 thomas patch_progress(void *arg, const char *old, const char *new,
6807 795b88cb 2023-07-10 thomas unsigned char status, const struct got_error *error, int old_from,
6808 795b88cb 2023-07-10 thomas int old_lines, int new_from, int new_lines, int offset,
6809 795b88cb 2023-07-10 thomas int ws_mangled, const struct got_error *hunk_err)
6810 795b88cb 2023-07-10 thomas {
6811 795b88cb 2023-07-10 thomas const char *path = new == NULL ? old : new;
6812 795b88cb 2023-07-10 thomas struct got_patch_progress_arg *a = arg;
6813 795b88cb 2023-07-10 thomas
6814 795b88cb 2023-07-10 thomas while (*path == '/')
6815 795b88cb 2023-07-10 thomas path++;
6816 795b88cb 2023-07-10 thomas
6817 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_NO_CHANGE &&
6818 795b88cb 2023-07-10 thomas status != 0 /* per-hunk progress */) {
6819 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
6820 795b88cb 2023-07-10 thomas a->did_something = 1;
6821 795b88cb 2023-07-10 thomas }
6822 795b88cb 2023-07-10 thomas
6823 795b88cb 2023-07-10 thomas if (hunk_err == NULL) {
6824 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
6825 795b88cb 2023-07-10 thomas a->rejects++;
6826 795b88cb 2023-07-10 thomas else if (status == GOT_STATUS_CONFLICT)
6827 795b88cb 2023-07-10 thomas a->conflicts++;
6828 795b88cb 2023-07-10 thomas }
6829 795b88cb 2023-07-10 thomas
6830 795b88cb 2023-07-10 thomas if (error != NULL)
6831 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6832 795b88cb 2023-07-10 thomas
6833 795b88cb 2023-07-10 thomas if (offset != 0 || hunk_err != NULL || ws_mangled) {
6834 795b88cb 2023-07-10 thomas printf("@@ -%d,%d +%d,%d @@ ", old_from,
6835 795b88cb 2023-07-10 thomas old_lines, new_from, new_lines);
6836 795b88cb 2023-07-10 thomas if (hunk_err != NULL)
6837 795b88cb 2023-07-10 thomas printf("%s\n", hunk_err->msg);
6838 795b88cb 2023-07-10 thomas else if (offset != 0)
6839 795b88cb 2023-07-10 thomas printf("applied with offset %d\n", offset);
6840 795b88cb 2023-07-10 thomas else
6841 795b88cb 2023-07-10 thomas printf("hunk contains mangled whitespace\n");
6842 795b88cb 2023-07-10 thomas }
6843 795b88cb 2023-07-10 thomas
6844 795b88cb 2023-07-10 thomas return NULL;
6845 795b88cb 2023-07-10 thomas }
6846 795b88cb 2023-07-10 thomas
6847 795b88cb 2023-07-10 thomas static void
6848 795b88cb 2023-07-10 thomas print_patch_progress_stats(struct got_patch_progress_arg *ppa)
6849 795b88cb 2023-07-10 thomas {
6850 795b88cb 2023-07-10 thomas if (!ppa->did_something)
6851 795b88cb 2023-07-10 thomas return;
6852 795b88cb 2023-07-10 thomas
6853 795b88cb 2023-07-10 thomas if (ppa->conflicts > 0)
6854 795b88cb 2023-07-10 thomas printf("Files with merge conflicts: %d\n", ppa->conflicts);
6855 795b88cb 2023-07-10 thomas
6856 795b88cb 2023-07-10 thomas if (ppa->rejects > 0) {
6857 795b88cb 2023-07-10 thomas printf("Files where patch failed to apply: %d\n",
6858 795b88cb 2023-07-10 thomas ppa->rejects);
6859 795b88cb 2023-07-10 thomas }
6860 795b88cb 2023-07-10 thomas }
6861 795b88cb 2023-07-10 thomas
6862 795b88cb 2023-07-10 thomas static const struct got_error *
6863 795b88cb 2023-07-10 thomas cmd_patch(int argc, char *argv[])
6864 795b88cb 2023-07-10 thomas {
6865 795b88cb 2023-07-10 thomas const struct got_error *error = NULL, *close_error = NULL;
6866 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6867 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6868 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
6869 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
6870 795b88cb 2023-07-10 thomas const char *commit_id_str = NULL;
6871 795b88cb 2023-07-10 thomas struct stat sb;
6872 795b88cb 2023-07-10 thomas const char *errstr;
6873 795b88cb 2023-07-10 thomas char *cwd = NULL;
6874 795b88cb 2023-07-10 thomas int ch, nop = 0, strip = -1, reverse = 0;
6875 795b88cb 2023-07-10 thomas int patchfd;
6876 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6877 795b88cb 2023-07-10 thomas struct got_patch_progress_arg ppa;
6878 795b88cb 2023-07-10 thomas
6879 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
6880 795b88cb 2023-07-10 thomas
6881 795b88cb 2023-07-10 thomas #ifndef PROFILE
6882 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
6883 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
6884 795b88cb 2023-07-10 thomas err(1, "pledge");
6885 795b88cb 2023-07-10 thomas #endif
6886 795b88cb 2023-07-10 thomas
6887 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
6888 795b88cb 2023-07-10 thomas switch (ch) {
6889 795b88cb 2023-07-10 thomas case 'c':
6890 795b88cb 2023-07-10 thomas commit_id_str = optarg;
6891 795b88cb 2023-07-10 thomas break;
6892 795b88cb 2023-07-10 thomas case 'n':
6893 795b88cb 2023-07-10 thomas nop = 1;
6894 795b88cb 2023-07-10 thomas break;
6895 795b88cb 2023-07-10 thomas case 'p':
6896 795b88cb 2023-07-10 thomas strip = strtonum(optarg, 0, INT_MAX, &errstr);
6897 795b88cb 2023-07-10 thomas if (errstr != NULL)
6898 795b88cb 2023-07-10 thomas errx(1, "pathname strip count is %s: %s",
6899 795b88cb 2023-07-10 thomas errstr, optarg);
6900 795b88cb 2023-07-10 thomas break;
6901 795b88cb 2023-07-10 thomas case 'R':
6902 795b88cb 2023-07-10 thomas reverse = 1;
6903 795b88cb 2023-07-10 thomas break;
6904 795b88cb 2023-07-10 thomas default:
6905 795b88cb 2023-07-10 thomas usage_patch();
6906 795b88cb 2023-07-10 thomas /* NOTREACHED */
6907 795b88cb 2023-07-10 thomas }
6908 795b88cb 2023-07-10 thomas }
6909 795b88cb 2023-07-10 thomas
6910 795b88cb 2023-07-10 thomas argc -= optind;
6911 795b88cb 2023-07-10 thomas argv += optind;
6912 795b88cb 2023-07-10 thomas
6913 795b88cb 2023-07-10 thomas if (argc == 0) {
6914 795b88cb 2023-07-10 thomas error = patch_from_stdin(&patchfd);
6915 795b88cb 2023-07-10 thomas if (error)
6916 795b88cb 2023-07-10 thomas return error;
6917 795b88cb 2023-07-10 thomas } else if (argc == 1) {
6918 795b88cb 2023-07-10 thomas patchfd = open(argv[0], O_RDONLY);
6919 795b88cb 2023-07-10 thomas if (patchfd == -1) {
6920 795b88cb 2023-07-10 thomas error = got_error_from_errno2("open", argv[0]);
6921 795b88cb 2023-07-10 thomas return error;
6922 795b88cb 2023-07-10 thomas }
6923 795b88cb 2023-07-10 thomas if (fstat(patchfd, &sb) == -1) {
6924 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fstat", argv[0]);
6925 795b88cb 2023-07-10 thomas goto done;
6926 795b88cb 2023-07-10 thomas }
6927 795b88cb 2023-07-10 thomas if (!S_ISREG(sb.st_mode)) {
6928 795b88cb 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
6929 795b88cb 2023-07-10 thomas goto done;
6930 795b88cb 2023-07-10 thomas }
6931 795b88cb 2023-07-10 thomas } else
6932 795b88cb 2023-07-10 thomas usage_patch();
6933 795b88cb 2023-07-10 thomas
6934 795b88cb 2023-07-10 thomas if ((cwd = getcwd(NULL, 0)) == NULL) {
6935 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6936 795b88cb 2023-07-10 thomas goto done;
6937 795b88cb 2023-07-10 thomas }
6938 795b88cb 2023-07-10 thomas
6939 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6940 795b88cb 2023-07-10 thomas if (error != NULL)
6941 795b88cb 2023-07-10 thomas goto done;
6942 795b88cb 2023-07-10 thomas
6943 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6944 795b88cb 2023-07-10 thomas if (error != NULL)
6945 795b88cb 2023-07-10 thomas goto done;
6946 795b88cb 2023-07-10 thomas
6947 795b88cb 2023-07-10 thomas const char *repo_path = got_worktree_get_repo_path(worktree);
6948 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6949 795b88cb 2023-07-10 thomas if (error != NULL)
6950 795b88cb 2023-07-10 thomas goto done;
6951 795b88cb 2023-07-10 thomas
6952 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
6953 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6954 795b88cb 2023-07-10 thomas if (error != NULL)
6955 795b88cb 2023-07-10 thomas goto done;
6956 795b88cb 2023-07-10 thomas
6957 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6958 795b88cb 2023-07-10 thomas if (error)
6959 795b88cb 2023-07-10 thomas goto done;
6960 795b88cb 2023-07-10 thomas
6961 795b88cb 2023-07-10 thomas if (commit_id_str != NULL) {
6962 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
6963 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6964 795b88cb 2023-07-10 thomas if (error)
6965 795b88cb 2023-07-10 thomas goto done;
6966 795b88cb 2023-07-10 thomas }
6967 795b88cb 2023-07-10 thomas
6968 795b88cb 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
6969 795b88cb 2023-07-10 thomas error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
6970 795b88cb 2023-07-10 thomas commit_id, patch_progress, &ppa, check_cancelled, NULL);
6971 795b88cb 2023-07-10 thomas print_patch_progress_stats(&ppa);
6972 795b88cb 2023-07-10 thomas done:
6973 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6974 795b88cb 2023-07-10 thomas free(commit_id);
6975 795b88cb 2023-07-10 thomas if (repo) {
6976 795b88cb 2023-07-10 thomas close_error = got_repo_close(repo);
6977 795b88cb 2023-07-10 thomas if (error == NULL)
6978 795b88cb 2023-07-10 thomas error = close_error;
6979 795b88cb 2023-07-10 thomas }
6980 795b88cb 2023-07-10 thomas if (worktree != NULL) {
6981 795b88cb 2023-07-10 thomas close_error = got_worktree_close(worktree);
6982 795b88cb 2023-07-10 thomas if (error == NULL)
6983 795b88cb 2023-07-10 thomas error = close_error;
6984 795b88cb 2023-07-10 thomas }
6985 795b88cb 2023-07-10 thomas if (pack_fds) {
6986 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6987 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6988 795b88cb 2023-07-10 thomas if (error == NULL)
6989 795b88cb 2023-07-10 thomas error = pack_err;
6990 795b88cb 2023-07-10 thomas }
6991 795b88cb 2023-07-10 thomas free(cwd);
6992 795b88cb 2023-07-10 thomas return error;
6993 795b88cb 2023-07-10 thomas }
6994 795b88cb 2023-07-10 thomas
6995 795b88cb 2023-07-10 thomas __dead static void
6996 795b88cb 2023-07-10 thomas usage_revert(void)
6997 795b88cb 2023-07-10 thomas {
6998 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
6999 795b88cb 2023-07-10 thomas getprogname());
7000 795b88cb 2023-07-10 thomas exit(1);
7001 795b88cb 2023-07-10 thomas }
7002 795b88cb 2023-07-10 thomas
7003 795b88cb 2023-07-10 thomas static const struct got_error *
7004 795b88cb 2023-07-10 thomas revert_progress(void *arg, unsigned char status, const char *path)
7005 795b88cb 2023-07-10 thomas {
7006 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
7007 795b88cb 2023-07-10 thomas return NULL;
7008 795b88cb 2023-07-10 thomas
7009 795b88cb 2023-07-10 thomas while (path[0] == '/')
7010 795b88cb 2023-07-10 thomas path++;
7011 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
7012 795b88cb 2023-07-10 thomas return NULL;
7013 795b88cb 2023-07-10 thomas }
7014 795b88cb 2023-07-10 thomas
7015 795b88cb 2023-07-10 thomas struct choose_patch_arg {
7016 795b88cb 2023-07-10 thomas FILE *patch_script_file;
7017 795b88cb 2023-07-10 thomas const char *action;
7018 795b88cb 2023-07-10 thomas };
7019 795b88cb 2023-07-10 thomas
7020 795b88cb 2023-07-10 thomas static const struct got_error *
7021 795b88cb 2023-07-10 thomas show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7022 795b88cb 2023-07-10 thomas int nchanges, const char *action)
7023 795b88cb 2023-07-10 thomas {
7024 795b88cb 2023-07-10 thomas const struct got_error *err;
7025 795b88cb 2023-07-10 thomas char *line = NULL;
7026 795b88cb 2023-07-10 thomas size_t linesize = 0;
7027 795b88cb 2023-07-10 thomas ssize_t linelen;
7028 795b88cb 2023-07-10 thomas
7029 795b88cb 2023-07-10 thomas switch (status) {
7030 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
7031 795b88cb 2023-07-10 thomas printf("A %s\n%s this addition? [y/n] ", path, action);
7032 795b88cb 2023-07-10 thomas break;
7033 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
7034 795b88cb 2023-07-10 thomas printf("D %s\n%s this deletion? [y/n] ", path, action);
7035 795b88cb 2023-07-10 thomas break;
7036 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
7037 795b88cb 2023-07-10 thomas if (fseek(patch_file, 0L, SEEK_SET) == -1)
7038 795b88cb 2023-07-10 thomas return got_error_from_errno("fseek");
7039 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
7040 795b88cb 2023-07-10 thomas while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7041 795b88cb 2023-07-10 thomas printf("%s", line);
7042 795b88cb 2023-07-10 thomas if (linelen == -1 && ferror(patch_file)) {
7043 795b88cb 2023-07-10 thomas err = got_error_from_errno("getline");
7044 795b88cb 2023-07-10 thomas free(line);
7045 795b88cb 2023-07-10 thomas return err;
7046 795b88cb 2023-07-10 thomas }
7047 795b88cb 2023-07-10 thomas free(line);
7048 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
7049 795b88cb 2023-07-10 thomas printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7050 795b88cb 2023-07-10 thomas path, n, nchanges, action);
7051 795b88cb 2023-07-10 thomas break;
7052 795b88cb 2023-07-10 thomas default:
7053 795b88cb 2023-07-10 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
7054 795b88cb 2023-07-10 thomas }
7055 795b88cb 2023-07-10 thomas
7056 795b88cb 2023-07-10 thomas fflush(stdout);
7057 795b88cb 2023-07-10 thomas return NULL;
7058 795b88cb 2023-07-10 thomas }
7059 795b88cb 2023-07-10 thomas
7060 795b88cb 2023-07-10 thomas static const struct got_error *
7061 795b88cb 2023-07-10 thomas choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7062 795b88cb 2023-07-10 thomas FILE *patch_file, int n, int nchanges)
7063 795b88cb 2023-07-10 thomas {
7064 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7065 795b88cb 2023-07-10 thomas char *line = NULL;
7066 795b88cb 2023-07-10 thomas size_t linesize = 0;
7067 795b88cb 2023-07-10 thomas ssize_t linelen;
7068 795b88cb 2023-07-10 thomas int resp = ' ';
7069 795b88cb 2023-07-10 thomas struct choose_patch_arg *a = arg;
7070 795b88cb 2023-07-10 thomas
7071 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NONE;
7072 795b88cb 2023-07-10 thomas
7073 795b88cb 2023-07-10 thomas if (a->patch_script_file) {
7074 795b88cb 2023-07-10 thomas char *nl;
7075 795b88cb 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
7076 795b88cb 2023-07-10 thomas a->action);
7077 795b88cb 2023-07-10 thomas if (err)
7078 795b88cb 2023-07-10 thomas return err;
7079 795b88cb 2023-07-10 thomas linelen = getline(&line, &linesize, a->patch_script_file);
7080 795b88cb 2023-07-10 thomas if (linelen == -1) {
7081 795b88cb 2023-07-10 thomas if (ferror(a->patch_script_file))
7082 795b88cb 2023-07-10 thomas return got_error_from_errno("getline");
7083 795b88cb 2023-07-10 thomas return NULL;
7084 795b88cb 2023-07-10 thomas }
7085 795b88cb 2023-07-10 thomas nl = strchr(line, '\n');
7086 795b88cb 2023-07-10 thomas if (nl)
7087 795b88cb 2023-07-10 thomas *nl = '\0';
7088 795b88cb 2023-07-10 thomas if (strcmp(line, "y") == 0) {
7089 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
7090 795b88cb 2023-07-10 thomas printf("y\n");
7091 795b88cb 2023-07-10 thomas } else if (strcmp(line, "n") == 0) {
7092 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
7093 795b88cb 2023-07-10 thomas printf("n\n");
7094 795b88cb 2023-07-10 thomas } else if (strcmp(line, "q") == 0 &&
7095 795b88cb 2023-07-10 thomas status == GOT_STATUS_MODIFY) {
7096 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
7097 795b88cb 2023-07-10 thomas printf("q\n");
7098 795b88cb 2023-07-10 thomas } else
7099 795b88cb 2023-07-10 thomas printf("invalid response '%s'\n", line);
7100 795b88cb 2023-07-10 thomas free(line);
7101 795b88cb 2023-07-10 thomas return NULL;
7102 795b88cb 2023-07-10 thomas }
7103 795b88cb 2023-07-10 thomas
7104 795b88cb 2023-07-10 thomas while (resp != 'y' && resp != 'n' && resp != 'q') {
7105 795b88cb 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
7106 795b88cb 2023-07-10 thomas a->action);
7107 795b88cb 2023-07-10 thomas if (err)
7108 795b88cb 2023-07-10 thomas return err;
7109 795b88cb 2023-07-10 thomas resp = getchar();
7110 795b88cb 2023-07-10 thomas if (resp == '\n')
7111 795b88cb 2023-07-10 thomas resp = getchar();
7112 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_MODIFY) {
7113 795b88cb 2023-07-10 thomas if (resp != 'y' && resp != 'n' && resp != 'q') {
7114 795b88cb 2023-07-10 thomas printf("invalid response '%c'\n", resp);
7115 795b88cb 2023-07-10 thomas resp = ' ';
7116 795b88cb 2023-07-10 thomas }
7117 795b88cb 2023-07-10 thomas } else if (resp != 'y' && resp != 'n') {
7118 795b88cb 2023-07-10 thomas printf("invalid response '%c'\n", resp);
7119 795b88cb 2023-07-10 thomas resp = ' ';
7120 795b88cb 2023-07-10 thomas }
7121 795b88cb 2023-07-10 thomas }
7122 795b88cb 2023-07-10 thomas
7123 795b88cb 2023-07-10 thomas if (resp == 'y')
7124 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
7125 795b88cb 2023-07-10 thomas else if (resp == 'n')
7126 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
7127 795b88cb 2023-07-10 thomas else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7128 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
7129 795b88cb 2023-07-10 thomas
7130 795b88cb 2023-07-10 thomas return NULL;
7131 795b88cb 2023-07-10 thomas }
7132 795b88cb 2023-07-10 thomas
7133 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg {
7134 795b88cb 2023-07-10 thomas struct got_pathlist_head *commit_paths;
7135 795b88cb 2023-07-10 thomas int *has_changes;
7136 795b88cb 2023-07-10 thomas };
7137 795b88cb 2023-07-10 thomas
7138 795b88cb 2023-07-10 thomas /*
7139 795b88cb 2023-07-10 thomas * Shortcut work tree status callback to determine if the set of paths scanned
7140 795b88cb 2023-07-10 thomas * has at least one versioned path that is being modified and, if not NULL, is
7141 795b88cb 2023-07-10 thomas * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
7142 795b88cb 2023-07-10 thomas * soon as a path is passed with a status that satisfies this criteria.
7143 795b88cb 2023-07-10 thomas */
7144 795b88cb 2023-07-10 thomas static const struct got_error *
7145 795b88cb 2023-07-10 thomas worktree_has_commitable_path(void *arg, unsigned char status,
7146 795b88cb 2023-07-10 thomas unsigned char staged_status, const char *path,
7147 795b88cb 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7148 795b88cb 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
7149 795b88cb 2023-07-10 thomas {
7150 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg *a = arg;
7151 795b88cb 2023-07-10 thomas
7152 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
7153 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
7154 795b88cb 2023-07-10 thomas
7155 795b88cb 2023-07-10 thomas if (!(status == GOT_STATUS_NO_CHANGE ||
7156 795b88cb 2023-07-10 thomas status == GOT_STATUS_UNVERSIONED) ||
7157 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_NO_CHANGE) {
7158 795b88cb 2023-07-10 thomas if (a->commit_paths != NULL) {
7159 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7160 795b88cb 2023-07-10 thomas
7161 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, a->commit_paths, entry) {
7162 795b88cb 2023-07-10 thomas if (strncmp(path, pe->path,
7163 795b88cb 2023-07-10 thomas pe->path_len) == 0) {
7164 795b88cb 2023-07-10 thomas *a->has_changes = 1;
7165 795b88cb 2023-07-10 thomas break;
7166 795b88cb 2023-07-10 thomas }
7167 795b88cb 2023-07-10 thomas }
7168 795b88cb 2023-07-10 thomas } else
7169 795b88cb 2023-07-10 thomas *a->has_changes = 1;
7170 795b88cb 2023-07-10 thomas
7171 795b88cb 2023-07-10 thomas if (*a->has_changes)
7172 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_FILE_MODIFIED);
7173 795b88cb 2023-07-10 thomas }
7174 795b88cb 2023-07-10 thomas
7175 795b88cb 2023-07-10 thomas return NULL;
7176 795b88cb 2023-07-10 thomas }
7177 795b88cb 2023-07-10 thomas
7178 795b88cb 2023-07-10 thomas /*
7179 795b88cb 2023-07-10 thomas * Check that the changeset of the commit identified by id is
7180 795b88cb 2023-07-10 thomas * comprised of at least one modified path that is being committed.
7181 795b88cb 2023-07-10 thomas */
7182 795b88cb 2023-07-10 thomas static const struct got_error *
7183 795b88cb 2023-07-10 thomas commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
7184 795b88cb 2023-07-10 thomas struct got_object_id *id, struct got_worktree *worktree,
7185 795b88cb 2023-07-10 thomas struct got_repository *repo)
7186 795b88cb 2023-07-10 thomas {
7187 795b88cb 2023-07-10 thomas const struct got_error *err;
7188 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7189 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL, *pcommit = NULL;
7190 795b88cb 2023-07-10 thomas struct got_tree_object *tree = NULL, *ptree = NULL;
7191 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
7192 795b88cb 2023-07-10 thomas
7193 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7194 795b88cb 2023-07-10 thomas
7195 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
7196 795b88cb 2023-07-10 thomas if (err)
7197 795b88cb 2023-07-10 thomas goto done;
7198 795b88cb 2023-07-10 thomas
7199 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo,
7200 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(commit));
7201 795b88cb 2023-07-10 thomas if (err)
7202 795b88cb 2023-07-10 thomas goto done;
7203 795b88cb 2023-07-10 thomas
7204 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7205 795b88cb 2023-07-10 thomas if (pid != NULL) {
7206 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo, &pid->id);
7207 795b88cb 2023-07-10 thomas if (err)
7208 795b88cb 2023-07-10 thomas goto done;
7209 795b88cb 2023-07-10 thomas
7210 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&ptree, repo,
7211 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
7212 795b88cb 2023-07-10 thomas if (err)
7213 795b88cb 2023-07-10 thomas goto done;
7214 795b88cb 2023-07-10 thomas }
7215 795b88cb 2023-07-10 thomas
7216 795b88cb 2023-07-10 thomas err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
7217 795b88cb 2023-07-10 thomas got_diff_tree_collect_changed_paths, &paths, 0);
7218 795b88cb 2023-07-10 thomas if (err)
7219 795b88cb 2023-07-10 thomas goto done;
7220 795b88cb 2023-07-10 thomas
7221 795b88cb 2023-07-10 thomas err = got_worktree_status(worktree, &paths, repo, 0,
7222 795b88cb 2023-07-10 thomas worktree_has_commitable_path, wcpa, check_cancelled, NULL);
7223 795b88cb 2023-07-10 thomas if (err && err->code == GOT_ERR_FILE_MODIFIED) {
7224 795b88cb 2023-07-10 thomas /*
7225 795b88cb 2023-07-10 thomas * At least one changed path in the referenced commit is
7226 795b88cb 2023-07-10 thomas * modified in the work tree, that's all we need to know!
7227 795b88cb 2023-07-10 thomas */
7228 795b88cb 2023-07-10 thomas err = NULL;
7229 795b88cb 2023-07-10 thomas }
7230 795b88cb 2023-07-10 thomas
7231 795b88cb 2023-07-10 thomas done:
7232 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
7233 795b88cb 2023-07-10 thomas if (commit)
7234 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7235 795b88cb 2023-07-10 thomas if (pcommit)
7236 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
7237 795b88cb 2023-07-10 thomas if (tree)
7238 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
7239 795b88cb 2023-07-10 thomas if (ptree)
7240 795b88cb 2023-07-10 thomas got_object_tree_close(ptree);
7241 795b88cb 2023-07-10 thomas return err;
7242 795b88cb 2023-07-10 thomas }
7243 795b88cb 2023-07-10 thomas
7244 795b88cb 2023-07-10 thomas /*
7245 795b88cb 2023-07-10 thomas * Remove any "logmsg" reference comprised entirely of paths that have
7246 795b88cb 2023-07-10 thomas * been reverted in this work tree. If any path in the logmsg ref changeset
7247 795b88cb 2023-07-10 thomas * remains in a changed state in the worktree, do not remove the reference.
7248 795b88cb 2023-07-10 thomas */
7249 795b88cb 2023-07-10 thomas static const struct got_error *
7250 795b88cb 2023-07-10 thomas rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
7251 795b88cb 2023-07-10 thomas {
7252 795b88cb 2023-07-10 thomas const struct got_error *err;
7253 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7254 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
7255 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
7256 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
7257 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
7258 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
7259 795b88cb 2023-07-10 thomas
7260 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7261 795b88cb 2023-07-10 thomas
7262 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
7263 795b88cb 2023-07-10 thomas if (err)
7264 795b88cb 2023-07-10 thomas goto done;
7265 795b88cb 2023-07-10 thomas
7266 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
7267 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
7268 795b88cb 2023-07-10 thomas if (err)
7269 795b88cb 2023-07-10 thomas goto done;
7270 795b88cb 2023-07-10 thomas
7271 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7272 795b88cb 2023-07-10 thomas const char *refname;
7273 795b88cb 2023-07-10 thomas int has_changes = 0;
7274 795b88cb 2023-07-10 thomas
7275 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7276 795b88cb 2023-07-10 thomas
7277 795b88cb 2023-07-10 thomas if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7278 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
7279 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7280 795b88cb 2023-07-10 thomas else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7281 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
7282 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7283 795b88cb 2023-07-10 thomas else
7284 795b88cb 2023-07-10 thomas continue;
7285 795b88cb 2023-07-10 thomas
7286 795b88cb 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7287 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7288 795b88cb 2023-07-10 thomas else
7289 795b88cb 2023-07-10 thomas continue;
7290 795b88cb 2023-07-10 thomas
7291 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, NULL, refname,
7292 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
7293 795b88cb 2023-07-10 thomas if (err)
7294 795b88cb 2023-07-10 thomas goto done;
7295 795b88cb 2023-07-10 thomas
7296 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
7297 795b88cb 2023-07-10 thomas if (err)
7298 795b88cb 2023-07-10 thomas goto done;
7299 795b88cb 2023-07-10 thomas
7300 795b88cb 2023-07-10 thomas wcpa.commit_paths = NULL;
7301 795b88cb 2023-07-10 thomas wcpa.has_changes = &has_changes;
7302 795b88cb 2023-07-10 thomas
7303 795b88cb 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, commit_id,
7304 795b88cb 2023-07-10 thomas worktree, repo);
7305 795b88cb 2023-07-10 thomas if (err)
7306 795b88cb 2023-07-10 thomas goto done;
7307 795b88cb 2023-07-10 thomas
7308 795b88cb 2023-07-10 thomas if (!has_changes) {
7309 795b88cb 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
7310 795b88cb 2023-07-10 thomas if (err)
7311 795b88cb 2023-07-10 thomas goto done;
7312 795b88cb 2023-07-10 thomas }
7313 795b88cb 2023-07-10 thomas
7314 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7315 795b88cb 2023-07-10 thomas commit = NULL;
7316 795b88cb 2023-07-10 thomas free(commit_id);
7317 795b88cb 2023-07-10 thomas commit_id = NULL;
7318 795b88cb 2023-07-10 thomas }
7319 795b88cb 2023-07-10 thomas
7320 795b88cb 2023-07-10 thomas done:
7321 795b88cb 2023-07-10 thomas free(uuidstr);
7322 795b88cb 2023-07-10 thomas free(commit_id);
7323 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
7324 795b88cb 2023-07-10 thomas if (commit)
7325 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7326 795b88cb 2023-07-10 thomas return err;
7327 795b88cb 2023-07-10 thomas }
7328 795b88cb 2023-07-10 thomas
7329 795b88cb 2023-07-10 thomas static const struct got_error *
7330 795b88cb 2023-07-10 thomas cmd_revert(int argc, char *argv[])
7331 795b88cb 2023-07-10 thomas {
7332 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
7333 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
7334 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
7335 795b88cb 2023-07-10 thomas char *cwd = NULL, *path = NULL;
7336 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7337 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7338 795b88cb 2023-07-10 thomas int ch, can_recurse = 0, pflag = 0;
7339 795b88cb 2023-07-10 thomas FILE *patch_script_file = NULL;
7340 795b88cb 2023-07-10 thomas const char *patch_script_path = NULL;
7341 795b88cb 2023-07-10 thomas struct choose_patch_arg cpa;
7342 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
7343 795b88cb 2023-07-10 thomas
7344 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7345 795b88cb 2023-07-10 thomas
7346 795b88cb 2023-07-10 thomas #ifndef PROFILE
7347 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7348 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
7349 795b88cb 2023-07-10 thomas err(1, "pledge");
7350 795b88cb 2023-07-10 thomas #endif
7351 795b88cb 2023-07-10 thomas
7352 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "F:pR")) != -1) {
7353 795b88cb 2023-07-10 thomas switch (ch) {
7354 795b88cb 2023-07-10 thomas case 'F':
7355 795b88cb 2023-07-10 thomas patch_script_path = optarg;
7356 795b88cb 2023-07-10 thomas break;
7357 795b88cb 2023-07-10 thomas case 'p':
7358 795b88cb 2023-07-10 thomas pflag = 1;
7359 795b88cb 2023-07-10 thomas break;
7360 795b88cb 2023-07-10 thomas case 'R':
7361 795b88cb 2023-07-10 thomas can_recurse = 1;
7362 795b88cb 2023-07-10 thomas break;
7363 795b88cb 2023-07-10 thomas default:
7364 795b88cb 2023-07-10 thomas usage_revert();
7365 795b88cb 2023-07-10 thomas /* NOTREACHED */
7366 795b88cb 2023-07-10 thomas }
7367 795b88cb 2023-07-10 thomas }
7368 795b88cb 2023-07-10 thomas
7369 795b88cb 2023-07-10 thomas argc -= optind;
7370 795b88cb 2023-07-10 thomas argv += optind;
7371 795b88cb 2023-07-10 thomas
7372 795b88cb 2023-07-10 thomas if (argc < 1)
7373 795b88cb 2023-07-10 thomas usage_revert();
7374 795b88cb 2023-07-10 thomas if (patch_script_path && !pflag)
7375 795b88cb 2023-07-10 thomas errx(1, "-F option can only be used together with -p option");
7376 795b88cb 2023-07-10 thomas
7377 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
7378 795b88cb 2023-07-10 thomas if (cwd == NULL) {
7379 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
7380 795b88cb 2023-07-10 thomas goto done;
7381 795b88cb 2023-07-10 thomas }
7382 795b88cb 2023-07-10 thomas
7383 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7384 795b88cb 2023-07-10 thomas if (error != NULL)
7385 795b88cb 2023-07-10 thomas goto done;
7386 795b88cb 2023-07-10 thomas
7387 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
7388 795b88cb 2023-07-10 thomas if (error) {
7389 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7390 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "revert", cwd);
7391 795b88cb 2023-07-10 thomas goto done;
7392 795b88cb 2023-07-10 thomas }
7393 795b88cb 2023-07-10 thomas
7394 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7395 795b88cb 2023-07-10 thomas NULL, pack_fds);
7396 795b88cb 2023-07-10 thomas if (error != NULL)
7397 795b88cb 2023-07-10 thomas goto done;
7398 795b88cb 2023-07-10 thomas
7399 795b88cb 2023-07-10 thomas if (patch_script_path) {
7400 795b88cb 2023-07-10 thomas patch_script_file = fopen(patch_script_path, "re");
7401 795b88cb 2023-07-10 thomas if (patch_script_file == NULL) {
7402 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fopen",
7403 795b88cb 2023-07-10 thomas patch_script_path);
7404 795b88cb 2023-07-10 thomas goto done;
7405 795b88cb 2023-07-10 thomas }
7406 795b88cb 2023-07-10 thomas }
7407 795b88cb 2023-07-10 thomas
7408 795b88cb 2023-07-10 thomas /*
7409 795b88cb 2023-07-10 thomas * XXX "c" perm needed on repo dir to delete merge references.
7410 795b88cb 2023-07-10 thomas */
7411 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
7412 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
7413 795b88cb 2023-07-10 thomas if (error)
7414 795b88cb 2023-07-10 thomas goto done;
7415 795b88cb 2023-07-10 thomas
7416 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7417 795b88cb 2023-07-10 thomas if (error)
7418 795b88cb 2023-07-10 thomas goto done;
7419 795b88cb 2023-07-10 thomas
7420 795b88cb 2023-07-10 thomas if (!can_recurse) {
7421 795b88cb 2023-07-10 thomas char *ondisk_path;
7422 795b88cb 2023-07-10 thomas struct stat sb;
7423 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
7424 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
7425 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
7426 795b88cb 2023-07-10 thomas pe->path) == -1) {
7427 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
7428 795b88cb 2023-07-10 thomas goto done;
7429 795b88cb 2023-07-10 thomas }
7430 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
7431 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
7432 795b88cb 2023-07-10 thomas free(ondisk_path);
7433 795b88cb 2023-07-10 thomas continue;
7434 795b88cb 2023-07-10 thomas }
7435 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
7436 795b88cb 2023-07-10 thomas ondisk_path);
7437 795b88cb 2023-07-10 thomas free(ondisk_path);
7438 795b88cb 2023-07-10 thomas goto done;
7439 795b88cb 2023-07-10 thomas }
7440 795b88cb 2023-07-10 thomas free(ondisk_path);
7441 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
7442 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
7443 795b88cb 2023-07-10 thomas "reverting directories requires -R option");
7444 795b88cb 2023-07-10 thomas goto done;
7445 795b88cb 2023-07-10 thomas }
7446 795b88cb 2023-07-10 thomas }
7447 795b88cb 2023-07-10 thomas }
7448 795b88cb 2023-07-10 thomas
7449 795b88cb 2023-07-10 thomas cpa.patch_script_file = patch_script_file;
7450 795b88cb 2023-07-10 thomas cpa.action = "revert";
7451 795b88cb 2023-07-10 thomas error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7452 795b88cb 2023-07-10 thomas pflag ? choose_patch : NULL, &cpa, repo);
7453 795b88cb 2023-07-10 thomas
7454 795b88cb 2023-07-10 thomas error = rm_logmsg_ref(worktree, repo);
7455 795b88cb 2023-07-10 thomas done:
7456 795b88cb 2023-07-10 thomas if (patch_script_file && fclose(patch_script_file) == EOF &&
7457 795b88cb 2023-07-10 thomas error == NULL)
7458 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fclose", patch_script_path);
7459 795b88cb 2023-07-10 thomas if (repo) {
7460 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
7461 795b88cb 2023-07-10 thomas if (error == NULL)
7462 795b88cb 2023-07-10 thomas error = close_err;
7463 795b88cb 2023-07-10 thomas }
7464 795b88cb 2023-07-10 thomas if (worktree)
7465 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
7466 795b88cb 2023-07-10 thomas if (pack_fds) {
7467 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
7468 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
7469 795b88cb 2023-07-10 thomas if (error == NULL)
7470 795b88cb 2023-07-10 thomas error = pack_err;
7471 795b88cb 2023-07-10 thomas }
7472 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7473 795b88cb 2023-07-10 thomas free(path);
7474 795b88cb 2023-07-10 thomas free(cwd);
7475 795b88cb 2023-07-10 thomas return error;
7476 795b88cb 2023-07-10 thomas }
7477 795b88cb 2023-07-10 thomas
7478 795b88cb 2023-07-10 thomas __dead static void
7479 795b88cb 2023-07-10 thomas usage_commit(void)
7480 795b88cb 2023-07-10 thomas {
7481 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
7482 795b88cb 2023-07-10 thomas "[-m message] [path ...]\n", getprogname());
7483 795b88cb 2023-07-10 thomas exit(1);
7484 795b88cb 2023-07-10 thomas }
7485 795b88cb 2023-07-10 thomas
7486 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg {
7487 795b88cb 2023-07-10 thomas const char *cmdline_log;
7488 795b88cb 2023-07-10 thomas const char *prepared_log;
7489 795b88cb 2023-07-10 thomas const char *merged_log;
7490 795b88cb 2023-07-10 thomas int non_interactive;
7491 795b88cb 2023-07-10 thomas const char *editor;
7492 795b88cb 2023-07-10 thomas const char *worktree_path;
7493 795b88cb 2023-07-10 thomas const char *repo_path;
7494 795b88cb 2023-07-10 thomas const char *dial_proto;
7495 795b88cb 2023-07-10 thomas char *logmsg_path;
7496 795b88cb 2023-07-10 thomas };
7497 795b88cb 2023-07-10 thomas
7498 795b88cb 2023-07-10 thomas static const struct got_error *
7499 795b88cb 2023-07-10 thomas read_prepared_logmsg(char **logmsg, const char *path)
7500 795b88cb 2023-07-10 thomas {
7501 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7502 795b88cb 2023-07-10 thomas FILE *f = NULL;
7503 795b88cb 2023-07-10 thomas struct stat sb;
7504 795b88cb 2023-07-10 thomas size_t r;
7505 795b88cb 2023-07-10 thomas
7506 795b88cb 2023-07-10 thomas *logmsg = NULL;
7507 795b88cb 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
7508 795b88cb 2023-07-10 thomas
7509 795b88cb 2023-07-10 thomas f = fopen(path, "re");
7510 795b88cb 2023-07-10 thomas if (f == NULL)
7511 795b88cb 2023-07-10 thomas return got_error_from_errno2("fopen", path);
7512 795b88cb 2023-07-10 thomas
7513 795b88cb 2023-07-10 thomas if (fstat(fileno(f), &sb) == -1) {
7514 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fstat", path);
7515 795b88cb 2023-07-10 thomas goto done;
7516 795b88cb 2023-07-10 thomas }
7517 795b88cb 2023-07-10 thomas if (sb.st_size == 0) {
7518 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7519 795b88cb 2023-07-10 thomas goto done;
7520 795b88cb 2023-07-10 thomas }
7521 795b88cb 2023-07-10 thomas
7522 795b88cb 2023-07-10 thomas *logmsg = malloc(sb.st_size + 1);
7523 795b88cb 2023-07-10 thomas if (*logmsg == NULL) {
7524 795b88cb 2023-07-10 thomas err = got_error_from_errno("malloc");
7525 795b88cb 2023-07-10 thomas goto done;
7526 795b88cb 2023-07-10 thomas }
7527 795b88cb 2023-07-10 thomas
7528 795b88cb 2023-07-10 thomas r = fread(*logmsg, 1, sb.st_size, f);
7529 795b88cb 2023-07-10 thomas if (r != sb.st_size) {
7530 795b88cb 2023-07-10 thomas if (ferror(f))
7531 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fread", path);
7532 795b88cb 2023-07-10 thomas else
7533 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_IO);
7534 795b88cb 2023-07-10 thomas goto done;
7535 795b88cb 2023-07-10 thomas }
7536 795b88cb 2023-07-10 thomas (*logmsg)[sb.st_size] = '\0';
7537 795b88cb 2023-07-10 thomas done:
7538 795b88cb 2023-07-10 thomas if (fclose(f) == EOF && err == NULL)
7539 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
7540 795b88cb 2023-07-10 thomas if (err) {
7541 795b88cb 2023-07-10 thomas free(*logmsg);
7542 795b88cb 2023-07-10 thomas *logmsg = NULL;
7543 795b88cb 2023-07-10 thomas }
7544 795b88cb 2023-07-10 thomas return err;
7545 795b88cb 2023-07-10 thomas }
7546 795b88cb 2023-07-10 thomas
7547 795b88cb 2023-07-10 thomas static const struct got_error *
7548 795b88cb 2023-07-10 thomas collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
7549 795b88cb 2023-07-10 thomas const char *diff_path, char **logmsg, void *arg)
7550 795b88cb 2023-07-10 thomas {
7551 795b88cb 2023-07-10 thomas char *initial_content = NULL;
7552 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7553 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7554 795b88cb 2023-07-10 thomas char *template = NULL;
7555 795b88cb 2023-07-10 thomas char *prepared_msg = NULL, *merged_msg = NULL;
7556 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg *a = arg;
7557 795b88cb 2023-07-10 thomas int initial_content_len;
7558 795b88cb 2023-07-10 thomas int fd = -1;
7559 795b88cb 2023-07-10 thomas size_t len;
7560 795b88cb 2023-07-10 thomas
7561 795b88cb 2023-07-10 thomas /* if a message was specified on the command line, just use it */
7562 795b88cb 2023-07-10 thomas if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
7563 795b88cb 2023-07-10 thomas len = strlen(a->cmdline_log) + 1;
7564 795b88cb 2023-07-10 thomas *logmsg = malloc(len + 1);
7565 795b88cb 2023-07-10 thomas if (*logmsg == NULL)
7566 795b88cb 2023-07-10 thomas return got_error_from_errno("malloc");
7567 795b88cb 2023-07-10 thomas strlcpy(*logmsg, a->cmdline_log, len);
7568 795b88cb 2023-07-10 thomas return NULL;
7569 795b88cb 2023-07-10 thomas } else if (a->prepared_log != NULL && a->non_interactive)
7570 795b88cb 2023-07-10 thomas return read_prepared_logmsg(logmsg, a->prepared_log);
7571 795b88cb 2023-07-10 thomas
7572 795b88cb 2023-07-10 thomas if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7573 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
7574 795b88cb 2023-07-10 thomas
7575 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
7576 795b88cb 2023-07-10 thomas if (err)
7577 795b88cb 2023-07-10 thomas goto done;
7578 795b88cb 2023-07-10 thomas
7579 795b88cb 2023-07-10 thomas if (a->prepared_log) {
7580 795b88cb 2023-07-10 thomas err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
7581 795b88cb 2023-07-10 thomas if (err)
7582 795b88cb 2023-07-10 thomas goto done;
7583 795b88cb 2023-07-10 thomas } else if (a->merged_log) {
7584 795b88cb 2023-07-10 thomas err = read_prepared_logmsg(&merged_msg, a->merged_log);
7585 795b88cb 2023-07-10 thomas if (err)
7586 795b88cb 2023-07-10 thomas goto done;
7587 795b88cb 2023-07-10 thomas }
7588 795b88cb 2023-07-10 thomas
7589 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
7590 795b88cb 2023-07-10 thomas "%s%s\n# changes to be committed:\n",
7591 795b88cb 2023-07-10 thomas prepared_msg ? prepared_msg : "",
7592 795b88cb 2023-07-10 thomas merged_msg ? merged_msg : "");
7593 795b88cb 2023-07-10 thomas if (initial_content_len == -1) {
7594 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
7595 795b88cb 2023-07-10 thomas goto done;
7596 795b88cb 2023-07-10 thomas }
7597 795b88cb 2023-07-10 thomas
7598 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
7599 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", a->logmsg_path);
7600 795b88cb 2023-07-10 thomas goto done;
7601 795b88cb 2023-07-10 thomas }
7602 795b88cb 2023-07-10 thomas
7603 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
7604 795b88cb 2023-07-10 thomas struct got_commitable *ct = pe->data;
7605 795b88cb 2023-07-10 thomas dprintf(fd, "# %c %s\n",
7606 795b88cb 2023-07-10 thomas got_commitable_get_status(ct),
7607 795b88cb 2023-07-10 thomas got_commitable_get_path(ct));
7608 795b88cb 2023-07-10 thomas }
7609 795b88cb 2023-07-10 thomas
7610 795b88cb 2023-07-10 thomas if (diff_path) {
7611 795b88cb 2023-07-10 thomas dprintf(fd, "# detailed changes can be viewed in %s\n",
7612 795b88cb 2023-07-10 thomas diff_path);
7613 795b88cb 2023-07-10 thomas }
7614 795b88cb 2023-07-10 thomas
7615 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
7616 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
7617 795b88cb 2023-07-10 thomas goto done;
7618 795b88cb 2023-07-10 thomas }
7619 795b88cb 2023-07-10 thomas fd = -1;
7620 795b88cb 2023-07-10 thomas
7621 795b88cb 2023-07-10 thomas err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7622 795b88cb 2023-07-10 thomas initial_content_len, a->prepared_log ? 0 : 1);
7623 795b88cb 2023-07-10 thomas done:
7624 795b88cb 2023-07-10 thomas free(initial_content);
7625 795b88cb 2023-07-10 thomas free(template);
7626 795b88cb 2023-07-10 thomas free(prepared_msg);
7627 795b88cb 2023-07-10 thomas free(merged_msg);
7628 795b88cb 2023-07-10 thomas
7629 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
7630 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
7631 795b88cb 2023-07-10 thomas
7632 795b88cb 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
7633 795b88cb 2023-07-10 thomas if (err == NULL)
7634 795b88cb 2023-07-10 thomas err = got_dial_apply_unveil(a->dial_proto);
7635 795b88cb 2023-07-10 thomas if (err == NULL)
7636 795b88cb 2023-07-10 thomas err = apply_unveil(a->repo_path, 0, a->worktree_path);
7637 795b88cb 2023-07-10 thomas if (err) {
7638 795b88cb 2023-07-10 thomas free(*logmsg);
7639 795b88cb 2023-07-10 thomas *logmsg = NULL;
7640 795b88cb 2023-07-10 thomas }
7641 795b88cb 2023-07-10 thomas return err;
7642 795b88cb 2023-07-10 thomas }
7643 795b88cb 2023-07-10 thomas
7644 795b88cb 2023-07-10 thomas static const struct got_error *
7645 795b88cb 2023-07-10 thomas cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
7646 795b88cb 2023-07-10 thomas const char *type, int has_content)
7647 795b88cb 2023-07-10 thomas {
7648 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7649 795b88cb 2023-07-10 thomas char *logmsg = NULL;
7650 795b88cb 2023-07-10 thomas
7651 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
7652 795b88cb 2023-07-10 thomas if (err)
7653 795b88cb 2023-07-10 thomas return err;
7654 795b88cb 2023-07-10 thomas
7655 795b88cb 2023-07-10 thomas if (fprintf(f, "%s# log message of %s commit %s:%s",
7656 795b88cb 2023-07-10 thomas has_content ? "\n" : "", type, idstr, logmsg) < 0)
7657 795b88cb 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
7658 795b88cb 2023-07-10 thomas
7659 795b88cb 2023-07-10 thomas free(logmsg);
7660 795b88cb 2023-07-10 thomas return err;
7661 795b88cb 2023-07-10 thomas }
7662 795b88cb 2023-07-10 thomas
7663 795b88cb 2023-07-10 thomas /*
7664 795b88cb 2023-07-10 thomas * Lookup "logmsg" references of backed-out and cherrypicked commits
7665 795b88cb 2023-07-10 thomas * belonging to the current work tree. If found, and the worktree has
7666 795b88cb 2023-07-10 thomas * at least one modified file that was changed in the referenced commit,
7667 795b88cb 2023-07-10 thomas * add its log message to a new temporary file at *logmsg_path.
7668 795b88cb 2023-07-10 thomas * Add all refs found to matched_refs to be scheduled for removal on
7669 795b88cb 2023-07-10 thomas * successful commit.
7670 795b88cb 2023-07-10 thomas */
7671 795b88cb 2023-07-10 thomas static const struct got_error *
7672 795b88cb 2023-07-10 thomas lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
7673 795b88cb 2023-07-10 thomas struct got_reflist_head *matched_refs, struct got_worktree *worktree,
7674 795b88cb 2023-07-10 thomas struct got_repository *repo)
7675 795b88cb 2023-07-10 thomas {
7676 795b88cb 2023-07-10 thomas const struct got_error *err;
7677 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
7678 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
7679 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7680 795b88cb 2023-07-10 thomas struct got_reflist_entry *re, *re_match;
7681 795b88cb 2023-07-10 thomas FILE *f = NULL;
7682 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
7683 795b88cb 2023-07-10 thomas int added_logmsg = 0;
7684 795b88cb 2023-07-10 thomas
7685 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7686 795b88cb 2023-07-10 thomas
7687 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
7688 795b88cb 2023-07-10 thomas
7689 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
7690 795b88cb 2023-07-10 thomas if (err)
7691 795b88cb 2023-07-10 thomas goto done;
7692 795b88cb 2023-07-10 thomas
7693 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
7694 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
7695 795b88cb 2023-07-10 thomas if (err)
7696 795b88cb 2023-07-10 thomas goto done;
7697 795b88cb 2023-07-10 thomas
7698 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7699 795b88cb 2023-07-10 thomas const char *refname, *type;
7700 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
7701 795b88cb 2023-07-10 thomas int add_logmsg = 0;
7702 795b88cb 2023-07-10 thomas
7703 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7704 795b88cb 2023-07-10 thomas
7705 795b88cb 2023-07-10 thomas if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7706 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
7707 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7708 795b88cb 2023-07-10 thomas type = "cherrypicked";
7709 795b88cb 2023-07-10 thomas } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7710 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
7711 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7712 795b88cb 2023-07-10 thomas type = "backed-out";
7713 795b88cb 2023-07-10 thomas } else
7714 795b88cb 2023-07-10 thomas continue;
7715 795b88cb 2023-07-10 thomas
7716 795b88cb 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7717 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7718 795b88cb 2023-07-10 thomas else
7719 795b88cb 2023-07-10 thomas continue;
7720 795b88cb 2023-07-10 thomas
7721 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
7722 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
7723 795b88cb 2023-07-10 thomas if (err)
7724 795b88cb 2023-07-10 thomas goto done;
7725 795b88cb 2023-07-10 thomas
7726 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
7727 795b88cb 2023-07-10 thomas if (err)
7728 795b88cb 2023-07-10 thomas goto done;
7729 795b88cb 2023-07-10 thomas
7730 795b88cb 2023-07-10 thomas wcpa.commit_paths = paths;
7731 795b88cb 2023-07-10 thomas wcpa.has_changes = &add_logmsg;
7732 795b88cb 2023-07-10 thomas
7733 795b88cb 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, id,
7734 795b88cb 2023-07-10 thomas worktree, repo);
7735 795b88cb 2023-07-10 thomas if (err)
7736 795b88cb 2023-07-10 thomas goto done;
7737 795b88cb 2023-07-10 thomas
7738 795b88cb 2023-07-10 thomas if (add_logmsg) {
7739 795b88cb 2023-07-10 thomas if (f == NULL) {
7740 795b88cb 2023-07-10 thomas err = got_opentemp_named(logmsg_path, &f,
7741 795b88cb 2023-07-10 thomas "got-commit-logmsg", "");
7742 795b88cb 2023-07-10 thomas if (err)
7743 795b88cb 2023-07-10 thomas goto done;
7744 795b88cb 2023-07-10 thomas }
7745 795b88cb 2023-07-10 thomas err = cat_logmsg(f, commit, refname, type,
7746 795b88cb 2023-07-10 thomas added_logmsg);
7747 795b88cb 2023-07-10 thomas if (err)
7748 795b88cb 2023-07-10 thomas goto done;
7749 795b88cb 2023-07-10 thomas if (!added_logmsg)
7750 795b88cb 2023-07-10 thomas ++added_logmsg;
7751 795b88cb 2023-07-10 thomas
7752 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&re_match, re);
7753 795b88cb 2023-07-10 thomas if (err)
7754 795b88cb 2023-07-10 thomas goto done;
7755 795b88cb 2023-07-10 thomas TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
7756 795b88cb 2023-07-10 thomas }
7757 795b88cb 2023-07-10 thomas
7758 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7759 795b88cb 2023-07-10 thomas commit = NULL;
7760 795b88cb 2023-07-10 thomas free(id);
7761 795b88cb 2023-07-10 thomas id = NULL;
7762 795b88cb 2023-07-10 thomas }
7763 795b88cb 2023-07-10 thomas
7764 795b88cb 2023-07-10 thomas done:
7765 795b88cb 2023-07-10 thomas free(id);
7766 795b88cb 2023-07-10 thomas free(uuidstr);
7767 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
7768 795b88cb 2023-07-10 thomas if (commit)
7769 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7770 795b88cb 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
7771 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
7772 795b88cb 2023-07-10 thomas if (!added_logmsg) {
7773 795b88cb 2023-07-10 thomas if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
7774 795b88cb 2023-07-10 thomas err = got_error_from_errno2("unlink", *logmsg_path);
7775 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
7776 795b88cb 2023-07-10 thomas }
7777 795b88cb 2023-07-10 thomas return err;
7778 795b88cb 2023-07-10 thomas }
7779 795b88cb 2023-07-10 thomas
7780 795b88cb 2023-07-10 thomas static const struct got_error *
7781 795b88cb 2023-07-10 thomas cmd_commit(int argc, char *argv[])
7782 795b88cb 2023-07-10 thomas {
7783 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
7784 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
7785 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
7786 795b88cb 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
7787 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
7788 795b88cb 2023-07-10 thomas const char *logmsg = NULL;
7789 795b88cb 2023-07-10 thomas char *prepared_logmsg = NULL, *merged_logmsg = NULL;
7790 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg cl_arg;
7791 795b88cb 2023-07-10 thomas const char *author = NULL;
7792 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
7793 795b88cb 2023-07-10 thomas int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7794 795b88cb 2023-07-10 thomas int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7795 795b88cb 2023-07-10 thomas int show_diff = 1, commit_conflicts = 0;
7796 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7797 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7798 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
7799 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
7800 795b88cb 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7801 795b88cb 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
7802 795b88cb 2023-07-10 thomas int nremotes;
7803 795b88cb 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
7804 795b88cb 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
7805 795b88cb 2023-07-10 thomas const char *remote_name;
7806 795b88cb 2023-07-10 thomas int verbosity = 0;
7807 795b88cb 2023-07-10 thomas int i;
7808 795b88cb 2023-07-10 thomas
7809 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7810 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7811 795b88cb 2023-07-10 thomas cl_arg.logmsg_path = NULL;
7812 795b88cb 2023-07-10 thomas
7813 795b88cb 2023-07-10 thomas #ifndef PROFILE
7814 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7815 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
7816 795b88cb 2023-07-10 thomas err(1, "pledge");
7817 795b88cb 2023-07-10 thomas #endif
7818 795b88cb 2023-07-10 thomas
7819 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
7820 795b88cb 2023-07-10 thomas switch (ch) {
7821 795b88cb 2023-07-10 thomas case 'A':
7822 795b88cb 2023-07-10 thomas author = optarg;
7823 795b88cb 2023-07-10 thomas error = valid_author(author);
7824 795b88cb 2023-07-10 thomas if (error)
7825 795b88cb 2023-07-10 thomas return error;
7826 795b88cb 2023-07-10 thomas break;
7827 795b88cb 2023-07-10 thomas case 'C':
7828 795b88cb 2023-07-10 thomas commit_conflicts = 1;
7829 795b88cb 2023-07-10 thomas break;
7830 795b88cb 2023-07-10 thomas case 'F':
7831 795b88cb 2023-07-10 thomas if (logmsg != NULL)
7832 795b88cb 2023-07-10 thomas option_conflict('F', 'm');
7833 795b88cb 2023-07-10 thomas prepared_logmsg = realpath(optarg, NULL);
7834 795b88cb 2023-07-10 thomas if (prepared_logmsg == NULL)
7835 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
7836 795b88cb 2023-07-10 thomas optarg);
7837 795b88cb 2023-07-10 thomas break;
7838 795b88cb 2023-07-10 thomas case 'm':
7839 795b88cb 2023-07-10 thomas if (prepared_logmsg)
7840 795b88cb 2023-07-10 thomas option_conflict('m', 'F');
7841 795b88cb 2023-07-10 thomas logmsg = optarg;
7842 795b88cb 2023-07-10 thomas break;
7843 795b88cb 2023-07-10 thomas case 'N':
7844 795b88cb 2023-07-10 thomas non_interactive = 1;
7845 795b88cb 2023-07-10 thomas break;
7846 795b88cb 2023-07-10 thomas case 'n':
7847 795b88cb 2023-07-10 thomas show_diff = 0;
7848 795b88cb 2023-07-10 thomas break;
7849 795b88cb 2023-07-10 thomas case 'S':
7850 795b88cb 2023-07-10 thomas allow_bad_symlinks = 1;
7851 795b88cb 2023-07-10 thomas break;
7852 795b88cb 2023-07-10 thomas default:
7853 795b88cb 2023-07-10 thomas usage_commit();
7854 795b88cb 2023-07-10 thomas /* NOTREACHED */
7855 795b88cb 2023-07-10 thomas }
7856 795b88cb 2023-07-10 thomas }
7857 795b88cb 2023-07-10 thomas
7858 795b88cb 2023-07-10 thomas argc -= optind;
7859 795b88cb 2023-07-10 thomas argv += optind;
7860 795b88cb 2023-07-10 thomas
7861 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
7862 795b88cb 2023-07-10 thomas if (cwd == NULL) {
7863 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
7864 795b88cb 2023-07-10 thomas goto done;
7865 795b88cb 2023-07-10 thomas }
7866 795b88cb 2023-07-10 thomas
7867 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7868 795b88cb 2023-07-10 thomas if (error != NULL)
7869 795b88cb 2023-07-10 thomas goto done;
7870 795b88cb 2023-07-10 thomas
7871 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
7872 795b88cb 2023-07-10 thomas if (error) {
7873 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7874 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "commit", cwd);
7875 795b88cb 2023-07-10 thomas goto done;
7876 795b88cb 2023-07-10 thomas }
7877 795b88cb 2023-07-10 thomas
7878 795b88cb 2023-07-10 thomas error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7879 795b88cb 2023-07-10 thomas if (error)
7880 795b88cb 2023-07-10 thomas goto done;
7881 795b88cb 2023-07-10 thomas if (rebase_in_progress) {
7882 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_REBASING);
7883 795b88cb 2023-07-10 thomas goto done;
7884 795b88cb 2023-07-10 thomas }
7885 795b88cb 2023-07-10 thomas
7886 795b88cb 2023-07-10 thomas error = got_worktree_histedit_in_progress(&histedit_in_progress,
7887 795b88cb 2023-07-10 thomas worktree);
7888 795b88cb 2023-07-10 thomas if (error)
7889 795b88cb 2023-07-10 thomas goto done;
7890 795b88cb 2023-07-10 thomas
7891 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
7892 795b88cb 2023-07-10 thomas if (error)
7893 795b88cb 2023-07-10 thomas goto done;
7894 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7895 795b88cb 2023-07-10 thomas gitconfig_path, pack_fds);
7896 795b88cb 2023-07-10 thomas if (error != NULL)
7897 795b88cb 2023-07-10 thomas goto done;
7898 795b88cb 2023-07-10 thomas
7899 795b88cb 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7900 795b88cb 2023-07-10 thomas if (error)
7901 795b88cb 2023-07-10 thomas goto done;
7902 795b88cb 2023-07-10 thomas if (merge_in_progress) {
7903 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
7904 795b88cb 2023-07-10 thomas goto done;
7905 795b88cb 2023-07-10 thomas }
7906 795b88cb 2023-07-10 thomas
7907 795b88cb 2023-07-10 thomas error = get_author(&committer, repo, worktree);
7908 795b88cb 2023-07-10 thomas if (error)
7909 795b88cb 2023-07-10 thomas goto done;
7910 795b88cb 2023-07-10 thomas
7911 795b88cb 2023-07-10 thomas if (author == NULL)
7912 795b88cb 2023-07-10 thomas author = committer;
7913 795b88cb 2023-07-10 thomas
7914 795b88cb 2023-07-10 thomas remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7915 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
7916 795b88cb 2023-07-10 thomas if (worktree_conf) {
7917 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
7918 795b88cb 2023-07-10 thomas worktree_conf);
7919 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7920 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7921 795b88cb 2023-07-10 thomas remote = &remotes[i];
7922 795b88cb 2023-07-10 thomas break;
7923 795b88cb 2023-07-10 thomas }
7924 795b88cb 2023-07-10 thomas }
7925 795b88cb 2023-07-10 thomas }
7926 795b88cb 2023-07-10 thomas if (remote == NULL) {
7927 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
7928 795b88cb 2023-07-10 thomas if (repo_conf) {
7929 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
7930 795b88cb 2023-07-10 thomas repo_conf);
7931 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7932 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7933 795b88cb 2023-07-10 thomas remote = &remotes[i];
7934 795b88cb 2023-07-10 thomas break;
7935 795b88cb 2023-07-10 thomas }
7936 795b88cb 2023-07-10 thomas }
7937 795b88cb 2023-07-10 thomas }
7938 795b88cb 2023-07-10 thomas }
7939 795b88cb 2023-07-10 thomas if (remote == NULL) {
7940 795b88cb 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7941 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7942 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7943 795b88cb 2023-07-10 thomas remote = &remotes[i];
7944 795b88cb 2023-07-10 thomas break;
7945 795b88cb 2023-07-10 thomas }
7946 795b88cb 2023-07-10 thomas }
7947 795b88cb 2023-07-10 thomas }
7948 795b88cb 2023-07-10 thomas if (remote == NULL) {
7949 795b88cb 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7950 795b88cb 2023-07-10 thomas goto done;
7951 795b88cb 2023-07-10 thomas }
7952 795b88cb 2023-07-10 thomas
7953 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7954 795b88cb 2023-07-10 thomas &repo_name, remote->fetch_url);
7955 795b88cb 2023-07-10 thomas if (error)
7956 795b88cb 2023-07-10 thomas goto done;
7957 795b88cb 2023-07-10 thomas
7958 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
7959 795b88cb 2023-07-10 thomas #ifndef PROFILE
7960 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7961 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
7962 795b88cb 2023-07-10 thomas err(1, "pledge");
7963 795b88cb 2023-07-10 thomas #endif
7964 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
7965 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
7966 795b88cb 2023-07-10 thomas #ifndef PROFILE
7967 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7968 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
7969 795b88cb 2023-07-10 thomas err(1, "pledge");
7970 795b88cb 2023-07-10 thomas #endif
7971 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
7972 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
7973 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7974 795b88cb 2023-07-10 thomas goto done;
7975 795b88cb 2023-07-10 thomas } else {
7976 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7977 795b88cb 2023-07-10 thomas goto done;
7978 795b88cb 2023-07-10 thomas }
7979 795b88cb 2023-07-10 thomas
7980 795b88cb 2023-07-10 thomas
7981 795b88cb 2023-07-10 thomas /*if (verbosity >= 0) {
7982 795b88cb 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
7983 795b88cb 2023-07-10 thomas remote->name, proto, host,
7984 795b88cb 2023-07-10 thomas port ? ":" : "", port ? port : "",
7985 795b88cb 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
7986 795b88cb 2023-07-10 thomas }*/
7987 795b88cb 2023-07-10 thomas
7988 795b88cb 2023-07-10 thomas /*
7989 795b88cb 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
7990 795b88cb 2023-07-10 thomas * to apply unveil after the log message has been written during
7991 795b88cb 2023-07-10 thomas * the callback.
7992 795b88cb 2023-07-10 thomas */
7993 795b88cb 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0)
7994 795b88cb 2023-07-10 thomas error = get_editor(&editor);
7995 795b88cb 2023-07-10 thomas else {
7996 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
7997 795b88cb 2023-07-10 thomas if (error)
7998 795b88cb 2023-07-10 thomas goto done;
7999 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8000 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
8001 795b88cb 2023-07-10 thomas }
8002 795b88cb 2023-07-10 thomas if (error)
8003 795b88cb 2023-07-10 thomas goto done;
8004 795b88cb 2023-07-10 thomas
8005 795b88cb 2023-07-10 thomas if (prepared_logmsg == NULL) {
8006 795b88cb 2023-07-10 thomas error = lookup_logmsg_ref(&merged_logmsg,
8007 795b88cb 2023-07-10 thomas argc > 0 ? &paths : NULL, &refs, worktree, repo);
8008 795b88cb 2023-07-10 thomas if (error)
8009 795b88cb 2023-07-10 thomas goto done;
8010 795b88cb 2023-07-10 thomas }
8011 795b88cb 2023-07-10 thomas
8012 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8013 795b88cb 2023-07-10 thomas if (error)
8014 795b88cb 2023-07-10 thomas goto done;
8015 795b88cb 2023-07-10 thomas
8016 795b88cb 2023-07-10 thomas cl_arg.editor = editor;
8017 795b88cb 2023-07-10 thomas cl_arg.cmdline_log = logmsg;
8018 795b88cb 2023-07-10 thomas cl_arg.prepared_log = prepared_logmsg;
8019 795b88cb 2023-07-10 thomas cl_arg.merged_log = merged_logmsg;
8020 795b88cb 2023-07-10 thomas cl_arg.non_interactive = non_interactive;
8021 795b88cb 2023-07-10 thomas cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8022 795b88cb 2023-07-10 thomas cl_arg.repo_path = got_repo_get_path(repo);
8023 795b88cb 2023-07-10 thomas cl_arg.dial_proto = proto;
8024 795b88cb 2023-07-10 thomas error = got_worktree_cvg_commit(&id, worktree, &paths, author,
8025 795b88cb 2023-07-10 thomas committer, allow_bad_symlinks, show_diff, commit_conflicts,
8026 795b88cb 2023-07-10 thomas collect_commit_logmsg, &cl_arg, print_status, NULL, proto, host,
8027 795b88cb 2023-07-10 thomas port, server_path, verbosity, remote, check_cancelled, repo);
8028 795b88cb 2023-07-10 thomas if (error) {
8029 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8030 795b88cb 2023-07-10 thomas cl_arg.logmsg_path != NULL)
8031 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
8032 795b88cb 2023-07-10 thomas goto done;
8033 795b88cb 2023-07-10 thomas }
8034 795b88cb 2023-07-10 thomas
8035 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, id);
8036 795b88cb 2023-07-10 thomas if (error)
8037 795b88cb 2023-07-10 thomas goto done;
8038 795b88cb 2023-07-10 thomas printf("Created commit %s\n", id_str);
8039 795b88cb 2023-07-10 thomas
8040 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8041 795b88cb 2023-07-10 thomas error = got_ref_delete(re->ref, repo);
8042 795b88cb 2023-07-10 thomas if (error)
8043 795b88cb 2023-07-10 thomas goto done;
8044 795b88cb 2023-07-10 thomas }
8045 795b88cb 2023-07-10 thomas
8046 795b88cb 2023-07-10 thomas done:
8047 795b88cb 2023-07-10 thomas if (preserve_logmsg) {
8048 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
8049 795b88cb 2023-07-10 thomas getprogname(), cl_arg.logmsg_path);
8050 795b88cb 2023-07-10 thomas } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8051 795b88cb 2023-07-10 thomas error == NULL)
8052 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8053 795b88cb 2023-07-10 thomas free(cl_arg.logmsg_path);
8054 795b88cb 2023-07-10 thomas if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
8055 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", merged_logmsg);
8056 795b88cb 2023-07-10 thomas free(merged_logmsg);
8057 795b88cb 2023-07-10 thomas if (repo) {
8058 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8059 795b88cb 2023-07-10 thomas if (error == NULL)
8060 795b88cb 2023-07-10 thomas error = close_err;
8061 795b88cb 2023-07-10 thomas }
8062 795b88cb 2023-07-10 thomas if (worktree)
8063 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8064 795b88cb 2023-07-10 thomas if (pack_fds) {
8065 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8066 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8067 795b88cb 2023-07-10 thomas if (error == NULL)
8068 795b88cb 2023-07-10 thomas error = pack_err;
8069 795b88cb 2023-07-10 thomas }
8070 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8071 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8072 795b88cb 2023-07-10 thomas free(cwd);
8073 795b88cb 2023-07-10 thomas free(id_str);
8074 795b88cb 2023-07-10 thomas free(gitconfig_path);
8075 795b88cb 2023-07-10 thomas free(editor);
8076 795b88cb 2023-07-10 thomas free(committer);
8077 795b88cb 2023-07-10 thomas free(prepared_logmsg);
8078 795b88cb 2023-07-10 thomas return error;
8079 795b88cb 2023-07-10 thomas }
8080 795b88cb 2023-07-10 thomas
8081 795b88cb 2023-07-10 thomas /*
8082 795b88cb 2023-07-10 thomas * Print and if delete is set delete all ref_prefix references.
8083 795b88cb 2023-07-10 thomas * If wanted_ref is not NULL, only print or delete this reference.
8084 795b88cb 2023-07-10 thomas */
8085 795b88cb 2023-07-10 thomas static const struct got_error *
8086 795b88cb 2023-07-10 thomas process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
8087 795b88cb 2023-07-10 thomas const char *wanted_ref, int delete, struct got_worktree *worktree,
8088 795b88cb 2023-07-10 thomas struct got_repository *repo)
8089 795b88cb 2023-07-10 thomas {
8090 795b88cb 2023-07-10 thomas const struct got_error *err;
8091 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
8092 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
8093 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
8094 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
8095 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8096 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
8097 795b88cb 2023-07-10 thomas const char *header_prefix;
8098 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
8099 795b88cb 2023-07-10 thomas int found = 0;
8100 795b88cb 2023-07-10 thomas
8101 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
8102 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
8103 795b88cb 2023-07-10 thomas
8104 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
8105 795b88cb 2023-07-10 thomas if (err)
8106 795b88cb 2023-07-10 thomas goto done;
8107 795b88cb 2023-07-10 thomas
8108 795b88cb 2023-07-10 thomas err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8109 795b88cb 2023-07-10 thomas if (err)
8110 795b88cb 2023-07-10 thomas goto done;
8111 795b88cb 2023-07-10 thomas
8112 795b88cb 2023-07-10 thomas if (worktree != NULL) {
8113 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
8114 795b88cb 2023-07-10 thomas if (err)
8115 795b88cb 2023-07-10 thomas goto done;
8116 795b88cb 2023-07-10 thomas }
8117 795b88cb 2023-07-10 thomas
8118 795b88cb 2023-07-10 thomas if (wanted_ref) {
8119 795b88cb 2023-07-10 thomas if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
8120 795b88cb 2023-07-10 thomas wanted_ref += 11;
8121 795b88cb 2023-07-10 thomas }
8122 795b88cb 2023-07-10 thomas
8123 795b88cb 2023-07-10 thomas if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
8124 795b88cb 2023-07-10 thomas header_prefix = "backout";
8125 795b88cb 2023-07-10 thomas else
8126 795b88cb 2023-07-10 thomas header_prefix = "cherrypick";
8127 795b88cb 2023-07-10 thomas
8128 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8129 795b88cb 2023-07-10 thomas const char *refname, *wt;
8130 795b88cb 2023-07-10 thomas
8131 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
8132 795b88cb 2023-07-10 thomas
8133 795b88cb 2023-07-10 thomas err = check_cancelled(NULL);
8134 795b88cb 2023-07-10 thomas if (err)
8135 795b88cb 2023-07-10 thomas goto done;
8136 795b88cb 2023-07-10 thomas
8137 795b88cb 2023-07-10 thomas if (strncmp(refname, ref_prefix, prefix_len) == 0)
8138 795b88cb 2023-07-10 thomas refname += prefix_len + 1; /* skip '-' delimiter */
8139 795b88cb 2023-07-10 thomas else
8140 795b88cb 2023-07-10 thomas continue;
8141 795b88cb 2023-07-10 thomas
8142 795b88cb 2023-07-10 thomas wt = refname;
8143 795b88cb 2023-07-10 thomas
8144 795b88cb 2023-07-10 thomas if (worktree == NULL || strncmp(refname, uuidstr,
8145 795b88cb 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN) == 0)
8146 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8147 795b88cb 2023-07-10 thomas else
8148 795b88cb 2023-07-10 thomas continue;
8149 795b88cb 2023-07-10 thomas
8150 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
8151 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8152 795b88cb 2023-07-10 thomas if (err)
8153 795b88cb 2023-07-10 thomas goto done;
8154 795b88cb 2023-07-10 thomas
8155 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
8156 795b88cb 2023-07-10 thomas if (err)
8157 795b88cb 2023-07-10 thomas goto done;
8158 795b88cb 2023-07-10 thomas
8159 795b88cb 2023-07-10 thomas if (wanted_ref)
8160 795b88cb 2023-07-10 thomas found = strncmp(wanted_ref, refname,
8161 795b88cb 2023-07-10 thomas strlen(wanted_ref)) == 0;
8162 795b88cb 2023-07-10 thomas if (wanted_ref && !found) {
8163 795b88cb 2023-07-10 thomas struct got_reflist_head *ci_refs;
8164 795b88cb 2023-07-10 thomas
8165 795b88cb 2023-07-10 thomas ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
8166 795b88cb 2023-07-10 thomas id);
8167 795b88cb 2023-07-10 thomas
8168 795b88cb 2023-07-10 thomas if (ci_refs) {
8169 795b88cb 2023-07-10 thomas char *refs_str = NULL;
8170 795b88cb 2023-07-10 thomas char const *r = NULL;
8171 795b88cb 2023-07-10 thomas
8172 795b88cb 2023-07-10 thomas err = build_refs_str(&refs_str, ci_refs, id,
8173 795b88cb 2023-07-10 thomas repo, 1);
8174 795b88cb 2023-07-10 thomas if (err)
8175 795b88cb 2023-07-10 thomas goto done;
8176 795b88cb 2023-07-10 thomas
8177 795b88cb 2023-07-10 thomas r = refs_str;
8178 795b88cb 2023-07-10 thomas while (r) {
8179 795b88cb 2023-07-10 thomas if (strncmp(r, wanted_ref,
8180 795b88cb 2023-07-10 thomas strlen(wanted_ref)) == 0) {
8181 795b88cb 2023-07-10 thomas found = 1;
8182 795b88cb 2023-07-10 thomas break;
8183 795b88cb 2023-07-10 thomas }
8184 795b88cb 2023-07-10 thomas r = strchr(r, ' ');
8185 795b88cb 2023-07-10 thomas if (r)
8186 795b88cb 2023-07-10 thomas ++r;
8187 795b88cb 2023-07-10 thomas }
8188 795b88cb 2023-07-10 thomas free(refs_str);
8189 795b88cb 2023-07-10 thomas }
8190 795b88cb 2023-07-10 thomas }
8191 795b88cb 2023-07-10 thomas
8192 795b88cb 2023-07-10 thomas if (wanted_ref == NULL || found) {
8193 795b88cb 2023-07-10 thomas if (delete) {
8194 795b88cb 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
8195 795b88cb 2023-07-10 thomas if (err)
8196 795b88cb 2023-07-10 thomas goto done;
8197 795b88cb 2023-07-10 thomas printf("Deleted: ");
8198 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, id, repo,
8199 795b88cb 2023-07-10 thomas refs_idmap);
8200 795b88cb 2023-07-10 thomas } else {
8201 795b88cb 2023-07-10 thomas /*
8202 795b88cb 2023-07-10 thomas * Print paths modified by commit to help
8203 795b88cb 2023-07-10 thomas * associate commits with worktree changes.
8204 795b88cb 2023-07-10 thomas */
8205 795b88cb 2023-07-10 thomas err = get_changed_paths(&paths, commit,
8206 795b88cb 2023-07-10 thomas repo, NULL);
8207 795b88cb 2023-07-10 thomas if (err)
8208 795b88cb 2023-07-10 thomas goto done;
8209 795b88cb 2023-07-10 thomas
8210 795b88cb 2023-07-10 thomas err = print_commit(commit, id, repo, NULL,
8211 795b88cb 2023-07-10 thomas &paths, NULL, 0, 0, refs_idmap, NULL,
8212 795b88cb 2023-07-10 thomas header_prefix);
8213 795b88cb 2023-07-10 thomas got_pathlist_free(&paths,
8214 795b88cb 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
8215 795b88cb 2023-07-10 thomas
8216 795b88cb 2023-07-10 thomas if (worktree == NULL)
8217 795b88cb 2023-07-10 thomas printf("work tree: %.*s\n\n",
8218 795b88cb 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN, wt);
8219 795b88cb 2023-07-10 thomas }
8220 795b88cb 2023-07-10 thomas if (err || found)
8221 795b88cb 2023-07-10 thomas goto done;
8222 795b88cb 2023-07-10 thomas }
8223 795b88cb 2023-07-10 thomas
8224 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8225 795b88cb 2023-07-10 thomas commit = NULL;
8226 795b88cb 2023-07-10 thomas free(id);
8227 795b88cb 2023-07-10 thomas id = NULL;
8228 795b88cb 2023-07-10 thomas }
8229 795b88cb 2023-07-10 thomas
8230 795b88cb 2023-07-10 thomas if (wanted_ref != NULL && !found)
8231 795b88cb 2023-07-10 thomas err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
8232 795b88cb 2023-07-10 thomas
8233 795b88cb 2023-07-10 thomas done:
8234 795b88cb 2023-07-10 thomas free(id);
8235 795b88cb 2023-07-10 thomas free(uuidstr);
8236 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8237 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8238 795b88cb 2023-07-10 thomas if (refs_idmap)
8239 795b88cb 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
8240 795b88cb 2023-07-10 thomas if (commit)
8241 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8242 795b88cb 2023-07-10 thomas return err;
8243 795b88cb 2023-07-10 thomas }
8244 795b88cb 2023-07-10 thomas
8245 795b88cb 2023-07-10 thomas /*
8246 795b88cb 2023-07-10 thomas * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
8247 795b88cb 2023-07-10 thomas * identified by id for log messages to prepopulate the editor on commit.
8248 795b88cb 2023-07-10 thomas */
8249 795b88cb 2023-07-10 thomas static const struct got_error *
8250 795b88cb 2023-07-10 thomas logmsg_ref(struct got_object_id *id, const char *prefix,
8251 795b88cb 2023-07-10 thomas struct got_worktree *worktree, struct got_repository *repo)
8252 795b88cb 2023-07-10 thomas {
8253 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
8254 795b88cb 2023-07-10 thomas char *idstr, *ref = NULL, *refname = NULL;
8255 795b88cb 2023-07-10 thomas int histedit_in_progress;
8256 795b88cb 2023-07-10 thomas int rebase_in_progress, merge_in_progress;
8257 795b88cb 2023-07-10 thomas
8258 795b88cb 2023-07-10 thomas /*
8259 795b88cb 2023-07-10 thomas * Silenty refuse to create merge reference if any histedit, merge,
8260 795b88cb 2023-07-10 thomas * or rebase operation is in progress.
8261 795b88cb 2023-07-10 thomas */
8262 795b88cb 2023-07-10 thomas err = got_worktree_histedit_in_progress(&histedit_in_progress,
8263 795b88cb 2023-07-10 thomas worktree);
8264 795b88cb 2023-07-10 thomas if (err)
8265 795b88cb 2023-07-10 thomas return err;
8266 795b88cb 2023-07-10 thomas if (histedit_in_progress)
8267 795b88cb 2023-07-10 thomas return NULL;
8268 795b88cb 2023-07-10 thomas
8269 795b88cb 2023-07-10 thomas err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8270 795b88cb 2023-07-10 thomas if (err)
8271 795b88cb 2023-07-10 thomas return err;
8272 795b88cb 2023-07-10 thomas if (rebase_in_progress)
8273 795b88cb 2023-07-10 thomas return NULL;
8274 795b88cb 2023-07-10 thomas
8275 795b88cb 2023-07-10 thomas err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
8276 795b88cb 2023-07-10 thomas repo);
8277 795b88cb 2023-07-10 thomas if (err)
8278 795b88cb 2023-07-10 thomas return err;
8279 795b88cb 2023-07-10 thomas if (merge_in_progress)
8280 795b88cb 2023-07-10 thomas return NULL;
8281 795b88cb 2023-07-10 thomas
8282 795b88cb 2023-07-10 thomas err = got_object_id_str(&idstr, id);
8283 795b88cb 2023-07-10 thomas if (err)
8284 795b88cb 2023-07-10 thomas return err;
8285 795b88cb 2023-07-10 thomas
8286 795b88cb 2023-07-10 thomas err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
8287 795b88cb 2023-07-10 thomas if (err)
8288 795b88cb 2023-07-10 thomas goto done;
8289 795b88cb 2023-07-10 thomas
8290 795b88cb 2023-07-10 thomas if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
8291 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
8292 795b88cb 2023-07-10 thomas goto done;
8293 795b88cb 2023-07-10 thomas }
8294 795b88cb 2023-07-10 thomas
8295 795b88cb 2023-07-10 thomas err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
8296 795b88cb 2023-07-10 thomas -1, repo);
8297 795b88cb 2023-07-10 thomas done:
8298 795b88cb 2023-07-10 thomas free(ref);
8299 795b88cb 2023-07-10 thomas free(idstr);
8300 795b88cb 2023-07-10 thomas free(refname);
8301 795b88cb 2023-07-10 thomas return err;
8302 795b88cb 2023-07-10 thomas }
8303 795b88cb 2023-07-10 thomas
8304 795b88cb 2023-07-10 thomas __dead static void
8305 795b88cb 2023-07-10 thomas usage_cherrypick(void)
8306 795b88cb 2023-07-10 thomas {
8307 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
8308 795b88cb 2023-07-10 thomas getprogname());
8309 795b88cb 2023-07-10 thomas exit(1);
8310 795b88cb 2023-07-10 thomas }
8311 795b88cb 2023-07-10 thomas
8312 795b88cb 2023-07-10 thomas static const struct got_error *
8313 795b88cb 2023-07-10 thomas cmd_cherrypick(int argc, char *argv[])
8314 795b88cb 2023-07-10 thomas {
8315 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
8316 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8317 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8318 795b88cb 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
8319 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8320 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8321 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8322 795b88cb 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
8323 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
8324 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8325 795b88cb 2023-07-10 thomas
8326 795b88cb 2023-07-10 thomas #ifndef PROFILE
8327 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8328 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
8329 795b88cb 2023-07-10 thomas err(1, "pledge");
8330 795b88cb 2023-07-10 thomas #endif
8331 795b88cb 2023-07-10 thomas
8332 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
8333 795b88cb 2023-07-10 thomas switch (ch) {
8334 795b88cb 2023-07-10 thomas case 'l':
8335 795b88cb 2023-07-10 thomas list_refs = 1;
8336 795b88cb 2023-07-10 thomas break;
8337 795b88cb 2023-07-10 thomas case 'X':
8338 795b88cb 2023-07-10 thomas remove_refs = 1;
8339 795b88cb 2023-07-10 thomas break;
8340 795b88cb 2023-07-10 thomas default:
8341 795b88cb 2023-07-10 thomas usage_cherrypick();
8342 795b88cb 2023-07-10 thomas /* NOTREACHED */
8343 795b88cb 2023-07-10 thomas }
8344 795b88cb 2023-07-10 thomas }
8345 795b88cb 2023-07-10 thomas
8346 795b88cb 2023-07-10 thomas argc -= optind;
8347 795b88cb 2023-07-10 thomas argv += optind;
8348 795b88cb 2023-07-10 thomas
8349 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8350 795b88cb 2023-07-10 thomas if (argc != 0 && argc != 1)
8351 795b88cb 2023-07-10 thomas usage_cherrypick();
8352 795b88cb 2023-07-10 thomas } else if (argc != 1)
8353 795b88cb 2023-07-10 thomas usage_cherrypick();
8354 795b88cb 2023-07-10 thomas if (list_refs && remove_refs)
8355 795b88cb 2023-07-10 thomas option_conflict('l', 'X');
8356 795b88cb 2023-07-10 thomas
8357 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8358 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8359 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8360 795b88cb 2023-07-10 thomas goto done;
8361 795b88cb 2023-07-10 thomas }
8362 795b88cb 2023-07-10 thomas
8363 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8364 795b88cb 2023-07-10 thomas if (error != NULL)
8365 795b88cb 2023-07-10 thomas goto done;
8366 795b88cb 2023-07-10 thomas
8367 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8368 795b88cb 2023-07-10 thomas if (error) {
8369 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8370 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
8371 795b88cb 2023-07-10 thomas goto done;
8372 795b88cb 2023-07-10 thomas } else {
8373 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8374 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error,
8375 795b88cb 2023-07-10 thomas "cherrypick", cwd);
8376 795b88cb 2023-07-10 thomas goto done;
8377 795b88cb 2023-07-10 thomas }
8378 795b88cb 2023-07-10 thomas }
8379 795b88cb 2023-07-10 thomas
8380 795b88cb 2023-07-10 thomas error = got_repo_open(&repo,
8381 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
8382 795b88cb 2023-07-10 thomas NULL, pack_fds);
8383 795b88cb 2023-07-10 thomas if (error != NULL)
8384 795b88cb 2023-07-10 thomas goto done;
8385 795b88cb 2023-07-10 thomas
8386 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8387 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
8388 795b88cb 2023-07-10 thomas if (error)
8389 795b88cb 2023-07-10 thomas goto done;
8390 795b88cb 2023-07-10 thomas
8391 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8392 795b88cb 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8393 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
8394 795b88cb 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8395 795b88cb 2023-07-10 thomas goto done;
8396 795b88cb 2023-07-10 thomas }
8397 795b88cb 2023-07-10 thomas
8398 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8399 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8400 795b88cb 2023-07-10 thomas if (error)
8401 795b88cb 2023-07-10 thomas goto done;
8402 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
8403 795b88cb 2023-07-10 thomas if (error)
8404 795b88cb 2023-07-10 thomas goto done;
8405 795b88cb 2023-07-10 thomas
8406 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8407 795b88cb 2023-07-10 thomas if (error)
8408 795b88cb 2023-07-10 thomas goto done;
8409 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8410 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
8411 795b88cb 2023-07-10 thomas error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
8412 795b88cb 2023-07-10 thomas commit_id, repo, update_progress, &upa, check_cancelled,
8413 795b88cb 2023-07-10 thomas NULL);
8414 795b88cb 2023-07-10 thomas if (error != NULL)
8415 795b88cb 2023-07-10 thomas goto done;
8416 795b88cb 2023-07-10 thomas
8417 795b88cb 2023-07-10 thomas if (upa.did_something) {
8418 795b88cb 2023-07-10 thomas error = logmsg_ref(commit_id,
8419 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
8420 795b88cb 2023-07-10 thomas if (error)
8421 795b88cb 2023-07-10 thomas goto done;
8422 795b88cb 2023-07-10 thomas printf("Merged commit %s\n", commit_id_str);
8423 795b88cb 2023-07-10 thomas }
8424 795b88cb 2023-07-10 thomas print_merge_progress_stats(&upa);
8425 795b88cb 2023-07-10 thomas done:
8426 795b88cb 2023-07-10 thomas free(cwd);
8427 795b88cb 2023-07-10 thomas if (commit)
8428 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8429 795b88cb 2023-07-10 thomas free(commit_id_str);
8430 795b88cb 2023-07-10 thomas if (worktree)
8431 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8432 795b88cb 2023-07-10 thomas if (repo) {
8433 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8434 795b88cb 2023-07-10 thomas if (error == NULL)
8435 795b88cb 2023-07-10 thomas error = close_err;
8436 795b88cb 2023-07-10 thomas }
8437 795b88cb 2023-07-10 thomas if (pack_fds) {
8438 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8439 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8440 795b88cb 2023-07-10 thomas if (error == NULL)
8441 795b88cb 2023-07-10 thomas error = pack_err;
8442 795b88cb 2023-07-10 thomas }
8443 795b88cb 2023-07-10 thomas
8444 795b88cb 2023-07-10 thomas return error;
8445 795b88cb 2023-07-10 thomas }
8446 795b88cb 2023-07-10 thomas
8447 795b88cb 2023-07-10 thomas __dead static void
8448 795b88cb 2023-07-10 thomas usage_backout(void)
8449 795b88cb 2023-07-10 thomas {
8450 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
8451 795b88cb 2023-07-10 thomas exit(1);
8452 795b88cb 2023-07-10 thomas }
8453 795b88cb 2023-07-10 thomas
8454 795b88cb 2023-07-10 thomas static const struct got_error *
8455 795b88cb 2023-07-10 thomas cmd_backout(int argc, char *argv[])
8456 795b88cb 2023-07-10 thomas {
8457 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
8458 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8459 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8460 795b88cb 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
8461 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8462 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8463 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8464 795b88cb 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
8465 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
8466 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8467 795b88cb 2023-07-10 thomas
8468 795b88cb 2023-07-10 thomas #ifndef PROFILE
8469 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8470 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
8471 795b88cb 2023-07-10 thomas err(1, "pledge");
8472 795b88cb 2023-07-10 thomas #endif
8473 795b88cb 2023-07-10 thomas
8474 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
8475 795b88cb 2023-07-10 thomas switch (ch) {
8476 795b88cb 2023-07-10 thomas case 'l':
8477 795b88cb 2023-07-10 thomas list_refs = 1;
8478 795b88cb 2023-07-10 thomas break;
8479 795b88cb 2023-07-10 thomas case 'X':
8480 795b88cb 2023-07-10 thomas remove_refs = 1;
8481 795b88cb 2023-07-10 thomas break;
8482 795b88cb 2023-07-10 thomas default:
8483 795b88cb 2023-07-10 thomas usage_backout();
8484 795b88cb 2023-07-10 thomas /* NOTREACHED */
8485 795b88cb 2023-07-10 thomas }
8486 795b88cb 2023-07-10 thomas }
8487 795b88cb 2023-07-10 thomas
8488 795b88cb 2023-07-10 thomas argc -= optind;
8489 795b88cb 2023-07-10 thomas argv += optind;
8490 795b88cb 2023-07-10 thomas
8491 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8492 795b88cb 2023-07-10 thomas if (argc != 0 && argc != 1)
8493 795b88cb 2023-07-10 thomas usage_backout();
8494 795b88cb 2023-07-10 thomas } else if (argc != 1)
8495 795b88cb 2023-07-10 thomas usage_backout();
8496 795b88cb 2023-07-10 thomas if (list_refs && remove_refs)
8497 795b88cb 2023-07-10 thomas option_conflict('l', 'X');
8498 795b88cb 2023-07-10 thomas
8499 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8500 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8501 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8502 795b88cb 2023-07-10 thomas goto done;
8503 795b88cb 2023-07-10 thomas }
8504 795b88cb 2023-07-10 thomas
8505 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8506 795b88cb 2023-07-10 thomas if (error != NULL)
8507 795b88cb 2023-07-10 thomas goto done;
8508 795b88cb 2023-07-10 thomas
8509 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8510 795b88cb 2023-07-10 thomas if (error) {
8511 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8512 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
8513 795b88cb 2023-07-10 thomas goto done;
8514 795b88cb 2023-07-10 thomas } else {
8515 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8516 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error,
8517 795b88cb 2023-07-10 thomas "backout", cwd);
8518 795b88cb 2023-07-10 thomas goto done;
8519 795b88cb 2023-07-10 thomas }
8520 795b88cb 2023-07-10 thomas }
8521 795b88cb 2023-07-10 thomas
8522 795b88cb 2023-07-10 thomas error = got_repo_open(&repo,
8523 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
8524 795b88cb 2023-07-10 thomas NULL, pack_fds);
8525 795b88cb 2023-07-10 thomas if (error != NULL)
8526 795b88cb 2023-07-10 thomas goto done;
8527 795b88cb 2023-07-10 thomas
8528 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8529 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
8530 795b88cb 2023-07-10 thomas if (error)
8531 795b88cb 2023-07-10 thomas goto done;
8532 795b88cb 2023-07-10 thomas
8533 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8534 795b88cb 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
8535 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
8536 795b88cb 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8537 795b88cb 2023-07-10 thomas goto done;
8538 795b88cb 2023-07-10 thomas }
8539 795b88cb 2023-07-10 thomas
8540 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8541 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8542 795b88cb 2023-07-10 thomas if (error)
8543 795b88cb 2023-07-10 thomas goto done;
8544 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
8545 795b88cb 2023-07-10 thomas if (error)
8546 795b88cb 2023-07-10 thomas goto done;
8547 795b88cb 2023-07-10 thomas
8548 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8549 795b88cb 2023-07-10 thomas if (error)
8550 795b88cb 2023-07-10 thomas goto done;
8551 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8552 795b88cb 2023-07-10 thomas if (pid == NULL) {
8553 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_ROOT_COMMIT);
8554 795b88cb 2023-07-10 thomas goto done;
8555 795b88cb 2023-07-10 thomas }
8556 795b88cb 2023-07-10 thomas
8557 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
8558 795b88cb 2023-07-10 thomas error = got_worktree_merge_files(worktree, commit_id, &pid->id,
8559 795b88cb 2023-07-10 thomas repo, update_progress, &upa, check_cancelled, NULL);
8560 795b88cb 2023-07-10 thomas if (error != NULL)
8561 795b88cb 2023-07-10 thomas goto done;
8562 795b88cb 2023-07-10 thomas
8563 795b88cb 2023-07-10 thomas if (upa.did_something) {
8564 795b88cb 2023-07-10 thomas error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8565 795b88cb 2023-07-10 thomas worktree, repo);
8566 795b88cb 2023-07-10 thomas if (error)
8567 795b88cb 2023-07-10 thomas goto done;
8568 795b88cb 2023-07-10 thomas printf("Backed out commit %s\n", commit_id_str);
8569 795b88cb 2023-07-10 thomas }
8570 795b88cb 2023-07-10 thomas print_merge_progress_stats(&upa);
8571 795b88cb 2023-07-10 thomas done:
8572 795b88cb 2023-07-10 thomas free(cwd);
8573 795b88cb 2023-07-10 thomas if (commit)
8574 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8575 795b88cb 2023-07-10 thomas free(commit_id_str);
8576 795b88cb 2023-07-10 thomas if (worktree)
8577 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8578 795b88cb 2023-07-10 thomas if (repo) {
8579 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8580 795b88cb 2023-07-10 thomas if (error == NULL)
8581 795b88cb 2023-07-10 thomas error = close_err;
8582 795b88cb 2023-07-10 thomas }
8583 795b88cb 2023-07-10 thomas if (pack_fds) {
8584 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8585 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8586 795b88cb 2023-07-10 thomas if (error == NULL)
8587 795b88cb 2023-07-10 thomas error = pack_err;
8588 795b88cb 2023-07-10 thomas }
8589 795b88cb 2023-07-10 thomas return error;
8590 795b88cb 2023-07-10 thomas }
8591 795b88cb 2023-07-10 thomas
8592 795b88cb 2023-07-10 thomas __dead static void
8593 795b88cb 2023-07-10 thomas usage_cat(void)
8594 795b88cb 2023-07-10 thomas {
8595 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
8596 795b88cb 2023-07-10 thomas "arg ...\n", getprogname());
8597 795b88cb 2023-07-10 thomas exit(1);
8598 795b88cb 2023-07-10 thomas }
8599 795b88cb 2023-07-10 thomas
8600 795b88cb 2023-07-10 thomas static const struct got_error *
8601 795b88cb 2023-07-10 thomas cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8602 795b88cb 2023-07-10 thomas {
8603 795b88cb 2023-07-10 thomas const struct got_error *err;
8604 795b88cb 2023-07-10 thomas struct got_blob_object *blob;
8605 795b88cb 2023-07-10 thomas int fd = -1;
8606 795b88cb 2023-07-10 thomas
8607 795b88cb 2023-07-10 thomas fd = got_opentempfd();
8608 795b88cb 2023-07-10 thomas if (fd == -1)
8609 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
8610 795b88cb 2023-07-10 thomas
8611 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
8612 795b88cb 2023-07-10 thomas if (err)
8613 795b88cb 2023-07-10 thomas goto done;
8614 795b88cb 2023-07-10 thomas
8615 795b88cb 2023-07-10 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8616 795b88cb 2023-07-10 thomas done:
8617 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
8618 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
8619 795b88cb 2023-07-10 thomas if (blob)
8620 795b88cb 2023-07-10 thomas got_object_blob_close(blob);
8621 795b88cb 2023-07-10 thomas return err;
8622 795b88cb 2023-07-10 thomas }
8623 795b88cb 2023-07-10 thomas
8624 795b88cb 2023-07-10 thomas static const struct got_error *
8625 795b88cb 2023-07-10 thomas cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8626 795b88cb 2023-07-10 thomas {
8627 795b88cb 2023-07-10 thomas const struct got_error *err;
8628 795b88cb 2023-07-10 thomas struct got_tree_object *tree;
8629 795b88cb 2023-07-10 thomas int nentries, i;
8630 795b88cb 2023-07-10 thomas
8631 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, id);
8632 795b88cb 2023-07-10 thomas if (err)
8633 795b88cb 2023-07-10 thomas return err;
8634 795b88cb 2023-07-10 thomas
8635 795b88cb 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
8636 795b88cb 2023-07-10 thomas for (i = 0; i < nentries; i++) {
8637 795b88cb 2023-07-10 thomas struct got_tree_entry *te;
8638 795b88cb 2023-07-10 thomas char *id_str;
8639 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
8640 795b88cb 2023-07-10 thomas break;
8641 795b88cb 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
8642 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8643 795b88cb 2023-07-10 thomas if (err)
8644 795b88cb 2023-07-10 thomas break;
8645 795b88cb 2023-07-10 thomas fprintf(outfile, "%s %.7o %s\n", id_str,
8646 795b88cb 2023-07-10 thomas got_tree_entry_get_mode(te),
8647 795b88cb 2023-07-10 thomas got_tree_entry_get_name(te));
8648 795b88cb 2023-07-10 thomas free(id_str);
8649 795b88cb 2023-07-10 thomas }
8650 795b88cb 2023-07-10 thomas
8651 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
8652 795b88cb 2023-07-10 thomas return err;
8653 795b88cb 2023-07-10 thomas }
8654 795b88cb 2023-07-10 thomas
8655 795b88cb 2023-07-10 thomas static const struct got_error *
8656 795b88cb 2023-07-10 thomas cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8657 795b88cb 2023-07-10 thomas {
8658 795b88cb 2023-07-10 thomas const struct got_error *err;
8659 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
8660 795b88cb 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
8661 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8662 795b88cb 2023-07-10 thomas char *id_str = NULL;
8663 795b88cb 2023-07-10 thomas const char *logmsg = NULL;
8664 795b88cb 2023-07-10 thomas char gmtoff[6];
8665 795b88cb 2023-07-10 thomas
8666 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
8667 795b88cb 2023-07-10 thomas if (err)
8668 795b88cb 2023-07-10 thomas return err;
8669 795b88cb 2023-07-10 thomas
8670 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8671 795b88cb 2023-07-10 thomas if (err)
8672 795b88cb 2023-07-10 thomas goto done;
8673 795b88cb 2023-07-10 thomas
8674 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8675 795b88cb 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
8676 795b88cb 2023-07-10 thomas fprintf(outfile, "numparents %d\n",
8677 795b88cb 2023-07-10 thomas got_object_commit_get_nparents(commit));
8678 795b88cb 2023-07-10 thomas STAILQ_FOREACH(pid, parent_ids, entry) {
8679 795b88cb 2023-07-10 thomas char *pid_str;
8680 795b88cb 2023-07-10 thomas err = got_object_id_str(&pid_str, &pid->id);
8681 795b88cb 2023-07-10 thomas if (err)
8682 795b88cb 2023-07-10 thomas goto done;
8683 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8684 795b88cb 2023-07-10 thomas free(pid_str);
8685 795b88cb 2023-07-10 thomas }
8686 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8687 795b88cb 2023-07-10 thomas got_object_commit_get_author_gmtoff(commit));
8688 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
8689 795b88cb 2023-07-10 thomas got_object_commit_get_author(commit),
8690 795b88cb 2023-07-10 thomas (long long)got_object_commit_get_author_time(commit),
8691 795b88cb 2023-07-10 thomas gmtoff);
8692 795b88cb 2023-07-10 thomas
8693 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8694 795b88cb 2023-07-10 thomas got_object_commit_get_committer_gmtoff(commit));
8695 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
8696 795b88cb 2023-07-10 thomas got_object_commit_get_committer(commit),
8697 795b88cb 2023-07-10 thomas (long long)got_object_commit_get_committer_time(commit),
8698 795b88cb 2023-07-10 thomas gmtoff);
8699 795b88cb 2023-07-10 thomas
8700 795b88cb 2023-07-10 thomas logmsg = got_object_commit_get_logmsg_raw(commit);
8701 795b88cb 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8702 795b88cb 2023-07-10 thomas fprintf(outfile, "%s", logmsg);
8703 795b88cb 2023-07-10 thomas done:
8704 795b88cb 2023-07-10 thomas free(id_str);
8705 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8706 795b88cb 2023-07-10 thomas return err;
8707 795b88cb 2023-07-10 thomas }
8708 795b88cb 2023-07-10 thomas
8709 795b88cb 2023-07-10 thomas static const struct got_error *
8710 795b88cb 2023-07-10 thomas cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8711 795b88cb 2023-07-10 thomas {
8712 795b88cb 2023-07-10 thomas const struct got_error *err;
8713 795b88cb 2023-07-10 thomas struct got_tag_object *tag;
8714 795b88cb 2023-07-10 thomas char *id_str = NULL;
8715 795b88cb 2023-07-10 thomas const char *tagmsg = NULL;
8716 795b88cb 2023-07-10 thomas char gmtoff[6];
8717 795b88cb 2023-07-10 thomas
8718 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
8719 795b88cb 2023-07-10 thomas if (err)
8720 795b88cb 2023-07-10 thomas return err;
8721 795b88cb 2023-07-10 thomas
8722 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8723 795b88cb 2023-07-10 thomas if (err)
8724 795b88cb 2023-07-10 thomas goto done;
8725 795b88cb 2023-07-10 thomas
8726 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8727 795b88cb 2023-07-10 thomas
8728 795b88cb 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
8729 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
8730 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8731 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_BLOB);
8732 795b88cb 2023-07-10 thomas break;
8733 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
8734 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8735 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_TREE);
8736 795b88cb 2023-07-10 thomas break;
8737 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
8738 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8739 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_COMMIT);
8740 795b88cb 2023-07-10 thomas break;
8741 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
8742 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8743 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_TAG);
8744 795b88cb 2023-07-10 thomas break;
8745 795b88cb 2023-07-10 thomas default:
8746 795b88cb 2023-07-10 thomas break;
8747 795b88cb 2023-07-10 thomas }
8748 795b88cb 2023-07-10 thomas
8749 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8750 795b88cb 2023-07-10 thomas got_object_tag_get_name(tag));
8751 795b88cb 2023-07-10 thomas
8752 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8753 795b88cb 2023-07-10 thomas got_object_tag_get_tagger_gmtoff(tag));
8754 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
8755 795b88cb 2023-07-10 thomas got_object_tag_get_tagger(tag),
8756 795b88cb 2023-07-10 thomas (long long)got_object_tag_get_tagger_time(tag),
8757 795b88cb 2023-07-10 thomas gmtoff);
8758 795b88cb 2023-07-10 thomas
8759 795b88cb 2023-07-10 thomas tagmsg = got_object_tag_get_message(tag);
8760 795b88cb 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8761 795b88cb 2023-07-10 thomas fprintf(outfile, "%s", tagmsg);
8762 795b88cb 2023-07-10 thomas done:
8763 795b88cb 2023-07-10 thomas free(id_str);
8764 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
8765 795b88cb 2023-07-10 thomas return err;
8766 795b88cb 2023-07-10 thomas }
8767 795b88cb 2023-07-10 thomas
8768 795b88cb 2023-07-10 thomas static const struct got_error *
8769 795b88cb 2023-07-10 thomas cmd_cat(int argc, char *argv[])
8770 795b88cb 2023-07-10 thomas {
8771 795b88cb 2023-07-10 thomas const struct got_error *error;
8772 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8773 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8774 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *label = NULL;
8775 795b88cb 2023-07-10 thomas const char *commit_id_str = NULL;
8776 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL, *commit_id = NULL;
8777 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8778 795b88cb 2023-07-10 thomas int ch, obj_type, i, force_path = 0;
8779 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
8780 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8781 795b88cb 2023-07-10 thomas
8782 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
8783 795b88cb 2023-07-10 thomas
8784 795b88cb 2023-07-10 thomas #ifndef PROFILE
8785 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8786 795b88cb 2023-07-10 thomas NULL) == -1)
8787 795b88cb 2023-07-10 thomas err(1, "pledge");
8788 795b88cb 2023-07-10 thomas #endif
8789 795b88cb 2023-07-10 thomas
8790 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
8791 795b88cb 2023-07-10 thomas switch (ch) {
8792 795b88cb 2023-07-10 thomas case 'c':
8793 795b88cb 2023-07-10 thomas commit_id_str = optarg;
8794 795b88cb 2023-07-10 thomas break;
8795 795b88cb 2023-07-10 thomas case 'P':
8796 795b88cb 2023-07-10 thomas force_path = 1;
8797 795b88cb 2023-07-10 thomas break;
8798 795b88cb 2023-07-10 thomas case 'r':
8799 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
8800 795b88cb 2023-07-10 thomas if (repo_path == NULL)
8801 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
8802 795b88cb 2023-07-10 thomas optarg);
8803 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
8804 795b88cb 2023-07-10 thomas break;
8805 795b88cb 2023-07-10 thomas default:
8806 795b88cb 2023-07-10 thomas usage_cat();
8807 795b88cb 2023-07-10 thomas /* NOTREACHED */
8808 795b88cb 2023-07-10 thomas }
8809 795b88cb 2023-07-10 thomas }
8810 795b88cb 2023-07-10 thomas
8811 795b88cb 2023-07-10 thomas argc -= optind;
8812 795b88cb 2023-07-10 thomas argv += optind;
8813 795b88cb 2023-07-10 thomas
8814 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8815 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8816 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8817 795b88cb 2023-07-10 thomas goto done;
8818 795b88cb 2023-07-10 thomas }
8819 795b88cb 2023-07-10 thomas
8820 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8821 795b88cb 2023-07-10 thomas if (error != NULL)
8822 795b88cb 2023-07-10 thomas goto done;
8823 795b88cb 2023-07-10 thomas
8824 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8825 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8826 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
8827 795b88cb 2023-07-10 thomas goto done;
8828 795b88cb 2023-07-10 thomas if (worktree) {
8829 795b88cb 2023-07-10 thomas repo_path = strdup(
8830 795b88cb 2023-07-10 thomas got_worktree_get_repo_path(worktree));
8831 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8832 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
8833 795b88cb 2023-07-10 thomas goto done;
8834 795b88cb 2023-07-10 thomas }
8835 795b88cb 2023-07-10 thomas
8836 795b88cb 2023-07-10 thomas /* Release work tree lock. */
8837 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8838 795b88cb 2023-07-10 thomas worktree = NULL;
8839 795b88cb 2023-07-10 thomas }
8840 795b88cb 2023-07-10 thomas }
8841 795b88cb 2023-07-10 thomas
8842 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8843 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
8844 795b88cb 2023-07-10 thomas if (repo_path == NULL)
8845 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
8846 795b88cb 2023-07-10 thomas }
8847 795b88cb 2023-07-10 thomas
8848 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8849 795b88cb 2023-07-10 thomas free(repo_path);
8850 795b88cb 2023-07-10 thomas if (error != NULL)
8851 795b88cb 2023-07-10 thomas goto done;
8852 795b88cb 2023-07-10 thomas
8853 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8854 795b88cb 2023-07-10 thomas if (error)
8855 795b88cb 2023-07-10 thomas goto done;
8856 795b88cb 2023-07-10 thomas
8857 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8858 795b88cb 2023-07-10 thomas if (error)
8859 795b88cb 2023-07-10 thomas goto done;
8860 795b88cb 2023-07-10 thomas
8861 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
8862 795b88cb 2023-07-10 thomas commit_id_str = GOT_REF_HEAD;
8863 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
8864 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8865 795b88cb 2023-07-10 thomas if (error)
8866 795b88cb 2023-07-10 thomas goto done;
8867 795b88cb 2023-07-10 thomas
8868 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8869 795b88cb 2023-07-10 thomas if (error)
8870 795b88cb 2023-07-10 thomas goto done;
8871 795b88cb 2023-07-10 thomas
8872 795b88cb 2023-07-10 thomas for (i = 0; i < argc; i++) {
8873 795b88cb 2023-07-10 thomas if (force_path) {
8874 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&id, repo, commit,
8875 795b88cb 2023-07-10 thomas argv[i]);
8876 795b88cb 2023-07-10 thomas if (error)
8877 795b88cb 2023-07-10 thomas break;
8878 795b88cb 2023-07-10 thomas } else {
8879 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&id, &label, argv[i],
8880 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
8881 795b88cb 2023-07-10 thomas repo);
8882 795b88cb 2023-07-10 thomas if (error) {
8883 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8884 795b88cb 2023-07-10 thomas error->code != GOT_ERR_NOT_REF)
8885 795b88cb 2023-07-10 thomas break;
8886 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&id, repo,
8887 795b88cb 2023-07-10 thomas commit, argv[i]);
8888 795b88cb 2023-07-10 thomas if (error)
8889 795b88cb 2023-07-10 thomas break;
8890 795b88cb 2023-07-10 thomas }
8891 795b88cb 2023-07-10 thomas }
8892 795b88cb 2023-07-10 thomas
8893 795b88cb 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, id);
8894 795b88cb 2023-07-10 thomas if (error)
8895 795b88cb 2023-07-10 thomas break;
8896 795b88cb 2023-07-10 thomas
8897 795b88cb 2023-07-10 thomas switch (obj_type) {
8898 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
8899 795b88cb 2023-07-10 thomas error = cat_blob(id, repo, stdout);
8900 795b88cb 2023-07-10 thomas break;
8901 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
8902 795b88cb 2023-07-10 thomas error = cat_tree(id, repo, stdout);
8903 795b88cb 2023-07-10 thomas break;
8904 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
8905 795b88cb 2023-07-10 thomas error = cat_commit(id, repo, stdout);
8906 795b88cb 2023-07-10 thomas break;
8907 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
8908 795b88cb 2023-07-10 thomas error = cat_tag(id, repo, stdout);
8909 795b88cb 2023-07-10 thomas break;
8910 795b88cb 2023-07-10 thomas default:
8911 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
8912 795b88cb 2023-07-10 thomas break;
8913 795b88cb 2023-07-10 thomas }
8914 795b88cb 2023-07-10 thomas if (error)
8915 795b88cb 2023-07-10 thomas break;
8916 795b88cb 2023-07-10 thomas free(label);
8917 795b88cb 2023-07-10 thomas label = NULL;
8918 795b88cb 2023-07-10 thomas free(id);
8919 795b88cb 2023-07-10 thomas id = NULL;
8920 795b88cb 2023-07-10 thomas }
8921 795b88cb 2023-07-10 thomas done:
8922 795b88cb 2023-07-10 thomas free(label);
8923 795b88cb 2023-07-10 thomas free(id);
8924 795b88cb 2023-07-10 thomas free(commit_id);
8925 795b88cb 2023-07-10 thomas if (commit)
8926 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8927 795b88cb 2023-07-10 thomas if (worktree)
8928 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8929 795b88cb 2023-07-10 thomas if (repo) {
8930 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8931 795b88cb 2023-07-10 thomas if (error == NULL)
8932 795b88cb 2023-07-10 thomas error = close_err;
8933 795b88cb 2023-07-10 thomas }
8934 795b88cb 2023-07-10 thomas if (pack_fds) {
8935 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8936 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8937 795b88cb 2023-07-10 thomas if (error == NULL)
8938 795b88cb 2023-07-10 thomas error = pack_err;
8939 795b88cb 2023-07-10 thomas }
8940 795b88cb 2023-07-10 thomas
8941 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8942 795b88cb 2023-07-10 thomas return error;
8943 795b88cb 2023-07-10 thomas }
8944 795b88cb 2023-07-10 thomas
8945 795b88cb 2023-07-10 thomas __dead static void
8946 795b88cb 2023-07-10 thomas usage_info(void)
8947 795b88cb 2023-07-10 thomas {
8948 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s info [path ...]\n",
8949 795b88cb 2023-07-10 thomas getprogname());
8950 795b88cb 2023-07-10 thomas exit(1);
8951 795b88cb 2023-07-10 thomas }
8952 795b88cb 2023-07-10 thomas
8953 795b88cb 2023-07-10 thomas static const struct got_error *
8954 795b88cb 2023-07-10 thomas print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
8955 795b88cb 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8956 795b88cb 2023-07-10 thomas struct got_object_id *commit_id)
8957 795b88cb 2023-07-10 thomas {
8958 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
8959 795b88cb 2023-07-10 thomas char *id_str = NULL;
8960 795b88cb 2023-07-10 thomas char datebuf[128];
8961 795b88cb 2023-07-10 thomas struct tm mytm, *tm;
8962 795b88cb 2023-07-10 thomas struct got_pathlist_head *paths = arg;
8963 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
8964 795b88cb 2023-07-10 thomas
8965 795b88cb 2023-07-10 thomas /*
8966 795b88cb 2023-07-10 thomas * Clear error indication from any of the path arguments which
8967 795b88cb 2023-07-10 thomas * would cause this file index entry to be displayed.
8968 795b88cb 2023-07-10 thomas */
8969 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
8970 795b88cb 2023-07-10 thomas if (got_path_cmp(path, pe->path, strlen(path),
8971 795b88cb 2023-07-10 thomas pe->path_len) == 0 ||
8972 795b88cb 2023-07-10 thomas got_path_is_child(path, pe->path, pe->path_len))
8973 795b88cb 2023-07-10 thomas pe->data = NULL; /* no error */
8974 795b88cb 2023-07-10 thomas }
8975 795b88cb 2023-07-10 thomas
8976 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
8977 795b88cb 2023-07-10 thomas if (S_ISLNK(mode))
8978 795b88cb 2023-07-10 thomas printf("symlink: %s\n", path);
8979 795b88cb 2023-07-10 thomas else if (S_ISREG(mode)) {
8980 795b88cb 2023-07-10 thomas printf("file: %s\n", path);
8981 795b88cb 2023-07-10 thomas printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
8982 795b88cb 2023-07-10 thomas } else if (S_ISDIR(mode))
8983 795b88cb 2023-07-10 thomas printf("directory: %s\n", path);
8984 795b88cb 2023-07-10 thomas else
8985 795b88cb 2023-07-10 thomas printf("something: %s\n", path);
8986 795b88cb 2023-07-10 thomas
8987 795b88cb 2023-07-10 thomas tm = localtime_r(&mtime, &mytm);
8988 795b88cb 2023-07-10 thomas if (tm == NULL)
8989 795b88cb 2023-07-10 thomas return NULL;
8990 795b88cb 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
8991 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
8992 795b88cb 2023-07-10 thomas printf("timestamp: %s\n", datebuf);
8993 795b88cb 2023-07-10 thomas
8994 795b88cb 2023-07-10 thomas if (blob_id) {
8995 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, blob_id);
8996 795b88cb 2023-07-10 thomas if (err)
8997 795b88cb 2023-07-10 thomas return err;
8998 795b88cb 2023-07-10 thomas printf("based on blob: %s\n", id_str);
8999 795b88cb 2023-07-10 thomas free(id_str);
9000 795b88cb 2023-07-10 thomas }
9001 795b88cb 2023-07-10 thomas
9002 795b88cb 2023-07-10 thomas if (staged_blob_id) {
9003 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
9004 795b88cb 2023-07-10 thomas if (err)
9005 795b88cb 2023-07-10 thomas return err;
9006 795b88cb 2023-07-10 thomas printf("based on staged blob: %s\n", id_str);
9007 795b88cb 2023-07-10 thomas free(id_str);
9008 795b88cb 2023-07-10 thomas }
9009 795b88cb 2023-07-10 thomas
9010 795b88cb 2023-07-10 thomas if (commit_id) {
9011 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, commit_id);
9012 795b88cb 2023-07-10 thomas if (err)
9013 795b88cb 2023-07-10 thomas return err;
9014 795b88cb 2023-07-10 thomas printf("based on commit: %s\n", id_str);
9015 795b88cb 2023-07-10 thomas free(id_str);
9016 795b88cb 2023-07-10 thomas }
9017 795b88cb 2023-07-10 thomas
9018 795b88cb 2023-07-10 thomas return NULL;
9019 795b88cb 2023-07-10 thomas }
9020 795b88cb 2023-07-10 thomas
9021 795b88cb 2023-07-10 thomas static const struct got_error *
9022 795b88cb 2023-07-10 thomas cmd_info(int argc, char *argv[])
9023 795b88cb 2023-07-10 thomas {
9024 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
9025 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
9026 795b88cb 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
9027 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
9028 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
9029 795b88cb 2023-07-10 thomas int ch, show_files = 0;
9030 795b88cb 2023-07-10 thomas
9031 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
9032 795b88cb 2023-07-10 thomas
9033 795b88cb 2023-07-10 thomas #ifndef PROFILE
9034 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9035 795b88cb 2023-07-10 thomas NULL) == -1)
9036 795b88cb 2023-07-10 thomas err(1, "pledge");
9037 795b88cb 2023-07-10 thomas #endif
9038 795b88cb 2023-07-10 thomas
9039 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "")) != -1) {
9040 795b88cb 2023-07-10 thomas switch (ch) {
9041 795b88cb 2023-07-10 thomas default:
9042 795b88cb 2023-07-10 thomas usage_info();
9043 795b88cb 2023-07-10 thomas /* NOTREACHED */
9044 795b88cb 2023-07-10 thomas }
9045 795b88cb 2023-07-10 thomas }
9046 795b88cb 2023-07-10 thomas
9047 795b88cb 2023-07-10 thomas argc -= optind;
9048 795b88cb 2023-07-10 thomas argv += optind;
9049 795b88cb 2023-07-10 thomas
9050 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
9051 795b88cb 2023-07-10 thomas if (cwd == NULL) {
9052 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
9053 795b88cb 2023-07-10 thomas goto done;
9054 795b88cb 2023-07-10 thomas }
9055 795b88cb 2023-07-10 thomas
9056 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
9057 795b88cb 2023-07-10 thomas if (error) {
9058 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
9059 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "info", cwd);
9060 795b88cb 2023-07-10 thomas goto done;
9061 795b88cb 2023-07-10 thomas }
9062 795b88cb 2023-07-10 thomas
9063 795b88cb 2023-07-10 thomas #ifndef PROFILE
9064 795b88cb 2023-07-10 thomas /* Remove "wpath cpath proc exec sendfd" promises. */
9065 795b88cb 2023-07-10 thomas if (pledge("stdio rpath flock unveil", NULL) == -1)
9066 795b88cb 2023-07-10 thomas err(1, "pledge");
9067 795b88cb 2023-07-10 thomas #endif
9068 795b88cb 2023-07-10 thomas error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9069 795b88cb 2023-07-10 thomas if (error)
9070 795b88cb 2023-07-10 thomas goto done;
9071 795b88cb 2023-07-10 thomas
9072 795b88cb 2023-07-10 thomas if (argc >= 1) {
9073 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
9074 795b88cb 2023-07-10 thomas worktree);
9075 795b88cb 2023-07-10 thomas if (error)
9076 795b88cb 2023-07-10 thomas goto done;
9077 795b88cb 2023-07-10 thomas show_files = 1;
9078 795b88cb 2023-07-10 thomas }
9079 795b88cb 2023-07-10 thomas
9080 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str,
9081 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
9082 795b88cb 2023-07-10 thomas if (error)
9083 795b88cb 2023-07-10 thomas goto done;
9084 795b88cb 2023-07-10 thomas
9085 795b88cb 2023-07-10 thomas error = got_worktree_get_uuid(&uuidstr, worktree);
9086 795b88cb 2023-07-10 thomas if (error)
9087 795b88cb 2023-07-10 thomas goto done;
9088 795b88cb 2023-07-10 thomas
9089 795b88cb 2023-07-10 thomas printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9090 795b88cb 2023-07-10 thomas printf("work tree base commit: %s\n", id_str);
9091 795b88cb 2023-07-10 thomas printf("work tree path prefix: %s\n",
9092 795b88cb 2023-07-10 thomas got_worktree_get_path_prefix(worktree));
9093 795b88cb 2023-07-10 thomas printf("work tree branch reference: %s\n",
9094 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
9095 795b88cb 2023-07-10 thomas printf("work tree UUID: %s\n", uuidstr);
9096 795b88cb 2023-07-10 thomas printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9097 795b88cb 2023-07-10 thomas
9098 795b88cb 2023-07-10 thomas if (show_files) {
9099 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
9100 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
9101 795b88cb 2023-07-10 thomas if (pe->path_len == 0)
9102 795b88cb 2023-07-10 thomas continue;
9103 795b88cb 2023-07-10 thomas /*
9104 795b88cb 2023-07-10 thomas * Assume this path will fail. This will be corrected
9105 795b88cb 2023-07-10 thomas * in print_path_info() in case the path does suceeed.
9106 795b88cb 2023-07-10 thomas */
9107 795b88cb 2023-07-10 thomas pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
9108 795b88cb 2023-07-10 thomas }
9109 795b88cb 2023-07-10 thomas error = got_worktree_path_info(worktree, &paths,
9110 795b88cb 2023-07-10 thomas print_path_info, &paths, check_cancelled, NULL);
9111 795b88cb 2023-07-10 thomas if (error)
9112 795b88cb 2023-07-10 thomas goto done;
9113 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
9114 795b88cb 2023-07-10 thomas if (pe->data != NULL) {
9115 795b88cb 2023-07-10 thomas const struct got_error *perr;
9116 795b88cb 2023-07-10 thomas
9117 795b88cb 2023-07-10 thomas perr = pe->data;
9118 795b88cb 2023-07-10 thomas error = got_error_fmt(perr->code, "%s",
9119 795b88cb 2023-07-10 thomas pe->path);
9120 795b88cb 2023-07-10 thomas break;
9121 795b88cb 2023-07-10 thomas }
9122 795b88cb 2023-07-10 thomas }
9123 795b88cb 2023-07-10 thomas }
9124 795b88cb 2023-07-10 thomas done:
9125 795b88cb 2023-07-10 thomas if (worktree)
9126 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
9127 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9128 795b88cb 2023-07-10 thomas free(cwd);
9129 795b88cb 2023-07-10 thomas free(id_str);
9130 795b88cb 2023-07-10 thomas free(uuidstr);
9131 795b88cb 2023-07-10 thomas return error;
9132 795b88cb 2023-07-10 thomas }