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 795b88cb 2023-07-10 thomas #endif
69 795b88cb 2023-07-10 thomas
70 795b88cb 2023-07-10 thomas static volatile sig_atomic_t sigint_received;
71 795b88cb 2023-07-10 thomas static volatile sig_atomic_t sigpipe_received;
72 795b88cb 2023-07-10 thomas
73 795b88cb 2023-07-10 thomas static void
74 795b88cb 2023-07-10 thomas catch_sigint(int signo)
75 795b88cb 2023-07-10 thomas {
76 795b88cb 2023-07-10 thomas sigint_received = 1;
77 795b88cb 2023-07-10 thomas }
78 795b88cb 2023-07-10 thomas
79 795b88cb 2023-07-10 thomas static void
80 795b88cb 2023-07-10 thomas catch_sigpipe(int signo)
81 795b88cb 2023-07-10 thomas {
82 795b88cb 2023-07-10 thomas sigpipe_received = 1;
83 795b88cb 2023-07-10 thomas }
84 795b88cb 2023-07-10 thomas
85 795b88cb 2023-07-10 thomas
86 795b88cb 2023-07-10 thomas struct got_cmd {
87 795b88cb 2023-07-10 thomas const char *cmd_name;
88 795b88cb 2023-07-10 thomas const struct got_error *(*cmd_main)(int, char *[]);
89 795b88cb 2023-07-10 thomas void (*cmd_usage)(void);
90 795b88cb 2023-07-10 thomas const char *cmd_alias;
91 795b88cb 2023-07-10 thomas };
92 795b88cb 2023-07-10 thomas
93 795b88cb 2023-07-10 thomas __dead static void usage(int, int);
94 795b88cb 2023-07-10 thomas __dead static void usage_import(void);
95 795b88cb 2023-07-10 thomas __dead static void usage_clone(void);
96 795b88cb 2023-07-10 thomas __dead static void usage_checkout(void);
97 795b88cb 2023-07-10 thomas __dead static void usage_update(void);
98 795b88cb 2023-07-10 thomas __dead static void usage_log(void);
99 795b88cb 2023-07-10 thomas __dead static void usage_diff(void);
100 795b88cb 2023-07-10 thomas __dead static void usage_blame(void);
101 795b88cb 2023-07-10 thomas __dead static void usage_tree(void);
102 795b88cb 2023-07-10 thomas __dead static void usage_status(void);
103 795b88cb 2023-07-10 thomas __dead static void usage_tag(void);
104 795b88cb 2023-07-10 thomas __dead static void usage_add(void);
105 795b88cb 2023-07-10 thomas __dead static void usage_remove(void);
106 795b88cb 2023-07-10 thomas __dead static void usage_patch(void);
107 795b88cb 2023-07-10 thomas __dead static void usage_revert(void);
108 795b88cb 2023-07-10 thomas __dead static void usage_commit(void);
109 795b88cb 2023-07-10 thomas __dead static void usage_cherrypick(void);
110 795b88cb 2023-07-10 thomas __dead static void usage_backout(void);
111 795b88cb 2023-07-10 thomas __dead static void usage_cat(void);
112 795b88cb 2023-07-10 thomas __dead static void usage_info(void);
113 795b88cb 2023-07-10 thomas
114 795b88cb 2023-07-10 thomas static const struct got_error* cmd_import(int, char *[]);
115 795b88cb 2023-07-10 thomas static const struct got_error* cmd_clone(int, char *[]);
116 795b88cb 2023-07-10 thomas static const struct got_error* cmd_checkout(int, char *[]);
117 795b88cb 2023-07-10 thomas static const struct got_error* cmd_update(int, char *[]);
118 795b88cb 2023-07-10 thomas static const struct got_error* cmd_log(int, char *[]);
119 795b88cb 2023-07-10 thomas static const struct got_error* cmd_diff(int, char *[]);
120 795b88cb 2023-07-10 thomas static const struct got_error* cmd_blame(int, char *[]);
121 795b88cb 2023-07-10 thomas static const struct got_error* cmd_tree(int, char *[]);
122 795b88cb 2023-07-10 thomas static const struct got_error* cmd_status(int, char *[]);
123 795b88cb 2023-07-10 thomas static const struct got_error* cmd_tag(int, char *[]);
124 795b88cb 2023-07-10 thomas static const struct got_error* cmd_add(int, char *[]);
125 795b88cb 2023-07-10 thomas static const struct got_error* cmd_remove(int, char *[]);
126 795b88cb 2023-07-10 thomas static const struct got_error* cmd_patch(int, char *[]);
127 795b88cb 2023-07-10 thomas static const struct got_error* cmd_revert(int, char *[]);
128 795b88cb 2023-07-10 thomas static const struct got_error* cmd_commit(int, char *[]);
129 795b88cb 2023-07-10 thomas static const struct got_error* cmd_cherrypick(int, char *[]);
130 795b88cb 2023-07-10 thomas static const struct got_error* cmd_backout(int, char *[]);
131 795b88cb 2023-07-10 thomas static const struct got_error* cmd_cat(int, char *[]);
132 795b88cb 2023-07-10 thomas static const struct got_error* cmd_info(int, char *[]);
133 795b88cb 2023-07-10 thomas
134 795b88cb 2023-07-10 thomas static const struct got_cmd got_commands[] = {
135 795b88cb 2023-07-10 thomas { "import", cmd_import, usage_import, "im" },
136 795b88cb 2023-07-10 thomas { "clone", cmd_clone, usage_clone, "cl" },
137 795b88cb 2023-07-10 thomas { "checkout", cmd_checkout, usage_checkout, "co" },
138 795b88cb 2023-07-10 thomas { "update", cmd_update, usage_update, "up" },
139 795b88cb 2023-07-10 thomas { "log", cmd_log, usage_log, "" },
140 795b88cb 2023-07-10 thomas { "diff", cmd_diff, usage_diff, "di" },
141 795b88cb 2023-07-10 thomas { "blame", cmd_blame, usage_blame, "bl" },
142 795b88cb 2023-07-10 thomas { "tree", cmd_tree, usage_tree, "tr" },
143 795b88cb 2023-07-10 thomas { "status", cmd_status, usage_status, "st" },
144 795b88cb 2023-07-10 thomas { "tag", cmd_tag, usage_tag, "" },
145 795b88cb 2023-07-10 thomas { "add", cmd_add, usage_add, "" },
146 795b88cb 2023-07-10 thomas { "remove", cmd_remove, usage_remove, "rm" },
147 795b88cb 2023-07-10 thomas { "patch", cmd_patch, usage_patch, "pa" },
148 795b88cb 2023-07-10 thomas { "revert", cmd_revert, usage_revert, "rv" },
149 795b88cb 2023-07-10 thomas { "commit", cmd_commit, usage_commit, "ci" },
150 795b88cb 2023-07-10 thomas { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 795b88cb 2023-07-10 thomas { "backout", cmd_backout, usage_backout, "bo" },
152 795b88cb 2023-07-10 thomas { "cat", cmd_cat, usage_cat, "" },
153 795b88cb 2023-07-10 thomas { "info", cmd_info, usage_info, "" },
154 795b88cb 2023-07-10 thomas };
155 795b88cb 2023-07-10 thomas
156 795b88cb 2023-07-10 thomas static void
157 795b88cb 2023-07-10 thomas list_commands(FILE *fp)
158 795b88cb 2023-07-10 thomas {
159 795b88cb 2023-07-10 thomas size_t i;
160 795b88cb 2023-07-10 thomas
161 795b88cb 2023-07-10 thomas fprintf(fp, "commands:");
162 795b88cb 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
163 795b88cb 2023-07-10 thomas const struct got_cmd *cmd = &got_commands[i];
164 795b88cb 2023-07-10 thomas fprintf(fp, " %s", cmd->cmd_name);
165 795b88cb 2023-07-10 thomas }
166 795b88cb 2023-07-10 thomas fputc('\n', fp);
167 795b88cb 2023-07-10 thomas }
168 795b88cb 2023-07-10 thomas
169 795b88cb 2023-07-10 thomas __dead static void
170 795b88cb 2023-07-10 thomas option_conflict(char a, char b)
171 795b88cb 2023-07-10 thomas {
172 795b88cb 2023-07-10 thomas errx(1, "-%c and -%c options are mutually exclusive", a, b);
173 795b88cb 2023-07-10 thomas }
174 795b88cb 2023-07-10 thomas
175 795b88cb 2023-07-10 thomas int
176 795b88cb 2023-07-10 thomas main(int argc, char *argv[])
177 795b88cb 2023-07-10 thomas {
178 795b88cb 2023-07-10 thomas const struct got_cmd *cmd;
179 795b88cb 2023-07-10 thomas size_t i;
180 795b88cb 2023-07-10 thomas int ch;
181 795b88cb 2023-07-10 thomas int hflag = 0, Vflag = 0;
182 795b88cb 2023-07-10 thomas static const struct option longopts[] = {
183 795b88cb 2023-07-10 thomas { "version", no_argument, NULL, 'V' },
184 795b88cb 2023-07-10 thomas { NULL, 0, NULL, 0 }
185 795b88cb 2023-07-10 thomas };
186 795b88cb 2023-07-10 thomas
187 795b88cb 2023-07-10 thomas setlocale(LC_CTYPE, "");
188 795b88cb 2023-07-10 thomas
189 795b88cb 2023-07-10 thomas while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
190 795b88cb 2023-07-10 thomas switch (ch) {
191 795b88cb 2023-07-10 thomas case 'h':
192 795b88cb 2023-07-10 thomas hflag = 1;
193 795b88cb 2023-07-10 thomas break;
194 795b88cb 2023-07-10 thomas case 'V':
195 795b88cb 2023-07-10 thomas Vflag = 1;
196 795b88cb 2023-07-10 thomas break;
197 795b88cb 2023-07-10 thomas default:
198 795b88cb 2023-07-10 thomas usage(hflag, 1);
199 795b88cb 2023-07-10 thomas /* NOTREACHED */
200 795b88cb 2023-07-10 thomas }
201 795b88cb 2023-07-10 thomas }
202 795b88cb 2023-07-10 thomas
203 795b88cb 2023-07-10 thomas argc -= optind;
204 795b88cb 2023-07-10 thomas argv += optind;
205 795b88cb 2023-07-10 thomas optind = 1;
206 795b88cb 2023-07-10 thomas optreset = 1;
207 795b88cb 2023-07-10 thomas
208 795b88cb 2023-07-10 thomas if (Vflag) {
209 795b88cb 2023-07-10 thomas got_version_print_str();
210 795b88cb 2023-07-10 thomas return 0;
211 795b88cb 2023-07-10 thomas }
212 795b88cb 2023-07-10 thomas
213 795b88cb 2023-07-10 thomas if (argc <= 0)
214 795b88cb 2023-07-10 thomas usage(hflag, hflag ? 0 : 1);
215 795b88cb 2023-07-10 thomas
216 795b88cb 2023-07-10 thomas signal(SIGINT, catch_sigint);
217 795b88cb 2023-07-10 thomas signal(SIGPIPE, catch_sigpipe);
218 795b88cb 2023-07-10 thomas
219 795b88cb 2023-07-10 thomas for (i = 0; i < nitems(got_commands); i++) {
220 795b88cb 2023-07-10 thomas const struct got_error *error;
221 795b88cb 2023-07-10 thomas
222 795b88cb 2023-07-10 thomas cmd = &got_commands[i];
223 795b88cb 2023-07-10 thomas
224 795b88cb 2023-07-10 thomas if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
225 795b88cb 2023-07-10 thomas strcmp(cmd->cmd_alias, argv[0]) != 0)
226 795b88cb 2023-07-10 thomas continue;
227 795b88cb 2023-07-10 thomas
228 795b88cb 2023-07-10 thomas if (hflag)
229 795b88cb 2023-07-10 thomas cmd->cmd_usage();
230 795b88cb 2023-07-10 thomas
231 795b88cb 2023-07-10 thomas error = cmd->cmd_main(argc, argv);
232 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_CANCELLED &&
233 795b88cb 2023-07-10 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
234 795b88cb 2023-07-10 thomas !(sigpipe_received &&
235 795b88cb 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
236 795b88cb 2023-07-10 thomas !(sigint_received &&
237 795b88cb 2023-07-10 thomas error->code == GOT_ERR_ERRNO && errno == EINTR)) {
238 795b88cb 2023-07-10 thomas fflush(stdout);
239 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
240 795b88cb 2023-07-10 thomas return 1;
241 795b88cb 2023-07-10 thomas }
242 795b88cb 2023-07-10 thomas
243 795b88cb 2023-07-10 thomas return 0;
244 795b88cb 2023-07-10 thomas }
245 795b88cb 2023-07-10 thomas
246 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
247 795b88cb 2023-07-10 thomas list_commands(stderr);
248 795b88cb 2023-07-10 thomas return 1;
249 795b88cb 2023-07-10 thomas }
250 795b88cb 2023-07-10 thomas
251 795b88cb 2023-07-10 thomas __dead static void
252 795b88cb 2023-07-10 thomas usage(int hflag, int status)
253 795b88cb 2023-07-10 thomas {
254 795b88cb 2023-07-10 thomas FILE *fp = (status == 0) ? stdout : stderr;
255 795b88cb 2023-07-10 thomas
256 795b88cb 2023-07-10 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
257 795b88cb 2023-07-10 thomas getprogname());
258 795b88cb 2023-07-10 thomas if (hflag)
259 795b88cb 2023-07-10 thomas list_commands(fp);
260 795b88cb 2023-07-10 thomas exit(status);
261 795b88cb 2023-07-10 thomas }
262 795b88cb 2023-07-10 thomas
263 795b88cb 2023-07-10 thomas static const struct got_error *
264 795b88cb 2023-07-10 thomas get_editor(char **abspath)
265 795b88cb 2023-07-10 thomas {
266 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
267 795b88cb 2023-07-10 thomas const char *editor;
268 795b88cb 2023-07-10 thomas
269 795b88cb 2023-07-10 thomas *abspath = NULL;
270 795b88cb 2023-07-10 thomas
271 795b88cb 2023-07-10 thomas editor = getenv("VISUAL");
272 795b88cb 2023-07-10 thomas if (editor == NULL)
273 795b88cb 2023-07-10 thomas editor = getenv("EDITOR");
274 795b88cb 2023-07-10 thomas
275 795b88cb 2023-07-10 thomas if (editor) {
276 795b88cb 2023-07-10 thomas err = got_path_find_prog(abspath, editor);
277 795b88cb 2023-07-10 thomas if (err)
278 795b88cb 2023-07-10 thomas return err;
279 795b88cb 2023-07-10 thomas }
280 795b88cb 2023-07-10 thomas
281 795b88cb 2023-07-10 thomas if (*abspath == NULL) {
282 795b88cb 2023-07-10 thomas *abspath = strdup("/usr/bin/vi");
283 795b88cb 2023-07-10 thomas if (*abspath == NULL)
284 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
285 795b88cb 2023-07-10 thomas }
286 795b88cb 2023-07-10 thomas
287 795b88cb 2023-07-10 thomas return NULL;
288 795b88cb 2023-07-10 thomas }
289 795b88cb 2023-07-10 thomas
290 795b88cb 2023-07-10 thomas static const struct got_error *
291 795b88cb 2023-07-10 thomas apply_unveil(const char *repo_path, int repo_read_only,
292 795b88cb 2023-07-10 thomas const char *worktree_path)
293 795b88cb 2023-07-10 thomas {
294 795b88cb 2023-07-10 thomas const struct got_error *err;
295 795b88cb 2023-07-10 thomas
296 795b88cb 2023-07-10 thomas #ifdef PROFILE
297 795b88cb 2023-07-10 thomas if (unveil("gmon.out", "rwc") != 0)
298 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", "gmon.out");
299 795b88cb 2023-07-10 thomas #endif
300 795b88cb 2023-07-10 thomas if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", repo_path);
302 795b88cb 2023-07-10 thomas
303 795b88cb 2023-07-10 thomas if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", worktree_path);
305 795b88cb 2023-07-10 thomas
306 795b88cb 2023-07-10 thomas if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 795b88cb 2023-07-10 thomas return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 795b88cb 2023-07-10 thomas
309 795b88cb 2023-07-10 thomas err = got_privsep_unveil_exec_helpers();
310 795b88cb 2023-07-10 thomas if (err != NULL)
311 795b88cb 2023-07-10 thomas return err;
312 795b88cb 2023-07-10 thomas
313 795b88cb 2023-07-10 thomas if (unveil(NULL, NULL) != 0)
314 795b88cb 2023-07-10 thomas return got_error_from_errno("unveil");
315 795b88cb 2023-07-10 thomas
316 795b88cb 2023-07-10 thomas return NULL;
317 795b88cb 2023-07-10 thomas }
318 795b88cb 2023-07-10 thomas
319 795b88cb 2023-07-10 thomas __dead static void
320 795b88cb 2023-07-10 thomas usage_import(void)
321 795b88cb 2023-07-10 thomas {
322 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
323 795b88cb 2023-07-10 thomas "[-r repository-path] directory\n", getprogname());
324 795b88cb 2023-07-10 thomas exit(1);
325 795b88cb 2023-07-10 thomas }
326 795b88cb 2023-07-10 thomas
327 795b88cb 2023-07-10 thomas static int
328 795b88cb 2023-07-10 thomas spawn_editor(const char *editor, const char *file)
329 795b88cb 2023-07-10 thomas {
330 795b88cb 2023-07-10 thomas pid_t pid;
331 795b88cb 2023-07-10 thomas sig_t sighup, sigint, sigquit;
332 795b88cb 2023-07-10 thomas int st = -1;
333 795b88cb 2023-07-10 thomas
334 795b88cb 2023-07-10 thomas sighup = signal(SIGHUP, SIG_IGN);
335 795b88cb 2023-07-10 thomas sigint = signal(SIGINT, SIG_IGN);
336 795b88cb 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_IGN);
337 795b88cb 2023-07-10 thomas
338 795b88cb 2023-07-10 thomas switch (pid = fork()) {
339 795b88cb 2023-07-10 thomas case -1:
340 795b88cb 2023-07-10 thomas goto doneediting;
341 795b88cb 2023-07-10 thomas case 0:
342 795b88cb 2023-07-10 thomas execl(editor, editor, file, (char *)NULL);
343 795b88cb 2023-07-10 thomas _exit(127);
344 795b88cb 2023-07-10 thomas }
345 795b88cb 2023-07-10 thomas
346 795b88cb 2023-07-10 thomas while (waitpid(pid, &st, 0) == -1)
347 795b88cb 2023-07-10 thomas if (errno != EINTR)
348 795b88cb 2023-07-10 thomas break;
349 795b88cb 2023-07-10 thomas
350 795b88cb 2023-07-10 thomas doneediting:
351 795b88cb 2023-07-10 thomas (void)signal(SIGHUP, sighup);
352 795b88cb 2023-07-10 thomas (void)signal(SIGINT, sigint);
353 795b88cb 2023-07-10 thomas (void)signal(SIGQUIT, sigquit);
354 795b88cb 2023-07-10 thomas
355 795b88cb 2023-07-10 thomas if (!WIFEXITED(st)) {
356 795b88cb 2023-07-10 thomas errno = EINTR;
357 795b88cb 2023-07-10 thomas return -1;
358 795b88cb 2023-07-10 thomas }
359 795b88cb 2023-07-10 thomas
360 795b88cb 2023-07-10 thomas return WEXITSTATUS(st);
361 795b88cb 2023-07-10 thomas }
362 795b88cb 2023-07-10 thomas
363 795b88cb 2023-07-10 thomas static const struct got_error *
364 795b88cb 2023-07-10 thomas read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
365 795b88cb 2023-07-10 thomas {
366 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
367 795b88cb 2023-07-10 thomas char *line = NULL;
368 795b88cb 2023-07-10 thomas size_t linesize = 0;
369 795b88cb 2023-07-10 thomas
370 795b88cb 2023-07-10 thomas *logmsg = NULL;
371 795b88cb 2023-07-10 thomas *len = 0;
372 795b88cb 2023-07-10 thomas
373 795b88cb 2023-07-10 thomas if (fseeko(fp, 0L, SEEK_SET) == -1)
374 795b88cb 2023-07-10 thomas return got_error_from_errno("fseeko");
375 795b88cb 2023-07-10 thomas
376 795b88cb 2023-07-10 thomas *logmsg = malloc(filesize + 1);
377 795b88cb 2023-07-10 thomas if (*logmsg == NULL)
378 795b88cb 2023-07-10 thomas return got_error_from_errno("malloc");
379 795b88cb 2023-07-10 thomas (*logmsg)[0] = '\0';
380 795b88cb 2023-07-10 thomas
381 795b88cb 2023-07-10 thomas while (getline(&line, &linesize, fp) != -1) {
382 795b88cb 2023-07-10 thomas if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
383 795b88cb 2023-07-10 thomas continue; /* remove comments and leading empty lines */
384 795b88cb 2023-07-10 thomas *len = strlcat(*logmsg, line, filesize + 1);
385 795b88cb 2023-07-10 thomas if (*len >= filesize + 1) {
386 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
387 795b88cb 2023-07-10 thomas goto done;
388 795b88cb 2023-07-10 thomas }
389 795b88cb 2023-07-10 thomas }
390 795b88cb 2023-07-10 thomas if (ferror(fp)) {
391 795b88cb 2023-07-10 thomas err = got_ferror(fp, GOT_ERR_IO);
392 795b88cb 2023-07-10 thomas goto done;
393 795b88cb 2023-07-10 thomas }
394 795b88cb 2023-07-10 thomas
395 795b88cb 2023-07-10 thomas while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
396 795b88cb 2023-07-10 thomas (*logmsg)[*len - 1] = '\0';
397 795b88cb 2023-07-10 thomas (*len)--;
398 795b88cb 2023-07-10 thomas }
399 795b88cb 2023-07-10 thomas done:
400 795b88cb 2023-07-10 thomas free(line);
401 795b88cb 2023-07-10 thomas if (err) {
402 795b88cb 2023-07-10 thomas free(*logmsg);
403 795b88cb 2023-07-10 thomas *logmsg = NULL;
404 795b88cb 2023-07-10 thomas *len = 0;
405 795b88cb 2023-07-10 thomas }
406 795b88cb 2023-07-10 thomas return err;
407 795b88cb 2023-07-10 thomas }
408 795b88cb 2023-07-10 thomas
409 795b88cb 2023-07-10 thomas static const struct got_error *
410 795b88cb 2023-07-10 thomas edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
411 795b88cb 2023-07-10 thomas const char *initial_content, size_t initial_content_len,
412 795b88cb 2023-07-10 thomas int require_modification)
413 795b88cb 2023-07-10 thomas {
414 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
415 795b88cb 2023-07-10 thomas struct stat st, st2;
416 795b88cb 2023-07-10 thomas FILE *fp = NULL;
417 795b88cb 2023-07-10 thomas size_t logmsg_len;
418 795b88cb 2023-07-10 thomas
419 795b88cb 2023-07-10 thomas *logmsg = NULL;
420 795b88cb 2023-07-10 thomas
421 795b88cb 2023-07-10 thomas if (stat(logmsg_path, &st) == -1)
422 795b88cb 2023-07-10 thomas return got_error_from_errno2("stat", logmsg_path);
423 795b88cb 2023-07-10 thomas
424 795b88cb 2023-07-10 thomas if (spawn_editor(editor, logmsg_path) == -1)
425 795b88cb 2023-07-10 thomas return got_error_from_errno("failed spawning editor");
426 795b88cb 2023-07-10 thomas
427 795b88cb 2023-07-10 thomas if (require_modification) {
428 795b88cb 2023-07-10 thomas struct timespec timeout;
429 795b88cb 2023-07-10 thomas
430 795b88cb 2023-07-10 thomas timeout.tv_sec = 0;
431 795b88cb 2023-07-10 thomas timeout.tv_nsec = 1;
432 795b88cb 2023-07-10 thomas nanosleep(&timeout, NULL);
433 795b88cb 2023-07-10 thomas }
434 795b88cb 2023-07-10 thomas
435 795b88cb 2023-07-10 thomas if (stat(logmsg_path, &st2) == -1)
436 5541355f 2023-07-17 thomas return got_error_from_errno2("stat", logmsg_path);
437 795b88cb 2023-07-10 thomas
438 795b88cb 2023-07-10 thomas if (require_modification && st.st_size == st2.st_size &&
439 795b88cb 2023-07-10 thomas timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
440 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 795b88cb 2023-07-10 thomas "no changes made to commit message, aborting");
442 795b88cb 2023-07-10 thomas
443 795b88cb 2023-07-10 thomas fp = fopen(logmsg_path, "re");
444 795b88cb 2023-07-10 thomas if (fp == NULL) {
445 795b88cb 2023-07-10 thomas err = got_error_from_errno("fopen");
446 795b88cb 2023-07-10 thomas goto done;
447 795b88cb 2023-07-10 thomas }
448 795b88cb 2023-07-10 thomas
449 795b88cb 2023-07-10 thomas /* strip comments and leading/trailing newlines */
450 795b88cb 2023-07-10 thomas err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
451 795b88cb 2023-07-10 thomas if (err)
452 795b88cb 2023-07-10 thomas goto done;
453 795b88cb 2023-07-10 thomas if (logmsg_len == 0) {
454 795b88cb 2023-07-10 thomas err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
455 795b88cb 2023-07-10 thomas "commit message cannot be empty, aborting");
456 795b88cb 2023-07-10 thomas goto done;
457 795b88cb 2023-07-10 thomas }
458 795b88cb 2023-07-10 thomas done:
459 795b88cb 2023-07-10 thomas if (fp && fclose(fp) == EOF && err == NULL)
460 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
461 795b88cb 2023-07-10 thomas if (err) {
462 795b88cb 2023-07-10 thomas free(*logmsg);
463 795b88cb 2023-07-10 thomas *logmsg = NULL;
464 795b88cb 2023-07-10 thomas }
465 795b88cb 2023-07-10 thomas return err;
466 795b88cb 2023-07-10 thomas }
467 795b88cb 2023-07-10 thomas
468 795b88cb 2023-07-10 thomas static const struct got_error *
469 795b88cb 2023-07-10 thomas collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
470 795b88cb 2023-07-10 thomas const char *path_dir, const char *branch_name)
471 795b88cb 2023-07-10 thomas {
472 795b88cb 2023-07-10 thomas char *initial_content = NULL;
473 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
474 795b88cb 2023-07-10 thomas int initial_content_len;
475 795b88cb 2023-07-10 thomas int fd = -1;
476 795b88cb 2023-07-10 thomas
477 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
478 795b88cb 2023-07-10 thomas "\n# %s to be imported to branch %s\n", path_dir,
479 795b88cb 2023-07-10 thomas branch_name);
480 795b88cb 2023-07-10 thomas if (initial_content_len == -1)
481 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
482 795b88cb 2023-07-10 thomas
483 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(logmsg_path, &fd,
484 795b88cb 2023-07-10 thomas GOT_TMPDIR_STR "/got-importmsg", "");
485 795b88cb 2023-07-10 thomas if (err)
486 795b88cb 2023-07-10 thomas goto done;
487 795b88cb 2023-07-10 thomas
488 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
489 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", *logmsg_path);
490 795b88cb 2023-07-10 thomas goto done;
491 795b88cb 2023-07-10 thomas }
492 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
493 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
494 795b88cb 2023-07-10 thomas goto done;
495 795b88cb 2023-07-10 thomas }
496 795b88cb 2023-07-10 thomas fd = -1;
497 795b88cb 2023-07-10 thomas
498 795b88cb 2023-07-10 thomas err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
499 795b88cb 2023-07-10 thomas initial_content_len, 1);
500 795b88cb 2023-07-10 thomas done:
501 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
502 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *logmsg_path);
503 795b88cb 2023-07-10 thomas free(initial_content);
504 795b88cb 2023-07-10 thomas if (err) {
505 795b88cb 2023-07-10 thomas free(*logmsg_path);
506 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
507 795b88cb 2023-07-10 thomas }
508 795b88cb 2023-07-10 thomas return err;
509 795b88cb 2023-07-10 thomas }
510 795b88cb 2023-07-10 thomas
511 795b88cb 2023-07-10 thomas static const struct got_error *
512 795b88cb 2023-07-10 thomas import_progress(void *arg, const char *path)
513 795b88cb 2023-07-10 thomas {
514 795b88cb 2023-07-10 thomas printf("A %s\n", path);
515 795b88cb 2023-07-10 thomas return NULL;
516 795b88cb 2023-07-10 thomas }
517 795b88cb 2023-07-10 thomas
518 795b88cb 2023-07-10 thomas static const struct got_error *
519 795b88cb 2023-07-10 thomas valid_author(const char *author)
520 795b88cb 2023-07-10 thomas {
521 795b88cb 2023-07-10 thomas const char *email = author;
522 795b88cb 2023-07-10 thomas
523 795b88cb 2023-07-10 thomas /*
524 795b88cb 2023-07-10 thomas * Git' expects the author (or committer) to be in the form
525 795b88cb 2023-07-10 thomas * "name <email>", which are mostly free form (see the
526 795b88cb 2023-07-10 thomas * "committer" description in git-fast-import(1)). We're only
527 795b88cb 2023-07-10 thomas * doing this to avoid git's object parser breaking on commits
528 795b88cb 2023-07-10 thomas * we create.
529 795b88cb 2023-07-10 thomas */
530 795b88cb 2023-07-10 thomas
531 795b88cb 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
532 795b88cb 2023-07-10 thomas author++;
533 795b88cb 2023-07-10 thomas if (author != email && *author == '<' && *(author - 1) != ' ')
534 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
535 795b88cb 2023-07-10 thomas "between author name and email required", email);
536 795b88cb 2023-07-10 thomas if (*author++ != '<')
537 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
538 795b88cb 2023-07-10 thomas while (*author && *author != '\n' && *author != '<' && *author != '>')
539 795b88cb 2023-07-10 thomas author++;
540 795b88cb 2023-07-10 thomas if (strcmp(author, ">") != 0)
541 795b88cb 2023-07-10 thomas return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
542 795b88cb 2023-07-10 thomas return NULL;
543 795b88cb 2023-07-10 thomas }
544 795b88cb 2023-07-10 thomas
545 795b88cb 2023-07-10 thomas static const struct got_error *
546 795b88cb 2023-07-10 thomas get_author(char **author, struct got_repository *repo,
547 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
548 795b88cb 2023-07-10 thomas {
549 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
550 795b88cb 2023-07-10 thomas const char *got_author = NULL, *name, *email;
551 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
552 795b88cb 2023-07-10 thomas
553 795b88cb 2023-07-10 thomas *author = NULL;
554 795b88cb 2023-07-10 thomas
555 795b88cb 2023-07-10 thomas if (worktree)
556 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
557 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
558 795b88cb 2023-07-10 thomas
559 795b88cb 2023-07-10 thomas /*
560 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
561 795b88cb 2023-07-10 thomas * significant to least significant:
562 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
563 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
564 795b88cb 2023-07-10 thomas * 3) repository's git config file
565 795b88cb 2023-07-10 thomas * 4) environment variables
566 795b88cb 2023-07-10 thomas * 5) global git config files (in user's home directory or /etc)
567 795b88cb 2023-07-10 thomas */
568 795b88cb 2023-07-10 thomas
569 795b88cb 2023-07-10 thomas if (worktree_conf)
570 795b88cb 2023-07-10 thomas got_author = got_gotconfig_get_author(worktree_conf);
571 795b88cb 2023-07-10 thomas if (got_author == NULL)
572 795b88cb 2023-07-10 thomas got_author = got_gotconfig_get_author(repo_conf);
573 795b88cb 2023-07-10 thomas if (got_author == NULL) {
574 795b88cb 2023-07-10 thomas name = got_repo_get_gitconfig_author_name(repo);
575 795b88cb 2023-07-10 thomas email = got_repo_get_gitconfig_author_email(repo);
576 795b88cb 2023-07-10 thomas if (name && email) {
577 795b88cb 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email) == -1)
578 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
579 795b88cb 2023-07-10 thomas return NULL;
580 795b88cb 2023-07-10 thomas }
581 795b88cb 2023-07-10 thomas
582 795b88cb 2023-07-10 thomas got_author = getenv("GOT_AUTHOR");
583 795b88cb 2023-07-10 thomas if (got_author == NULL) {
584 795b88cb 2023-07-10 thomas name = got_repo_get_global_gitconfig_author_name(repo);
585 795b88cb 2023-07-10 thomas email = got_repo_get_global_gitconfig_author_email(
586 795b88cb 2023-07-10 thomas repo);
587 795b88cb 2023-07-10 thomas if (name && email) {
588 795b88cb 2023-07-10 thomas if (asprintf(author, "%s <%s>", name, email)
589 795b88cb 2023-07-10 thomas == -1)
590 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
591 795b88cb 2023-07-10 thomas return NULL;
592 795b88cb 2023-07-10 thomas }
593 795b88cb 2023-07-10 thomas /* TODO: Look up user in password database? */
594 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
595 795b88cb 2023-07-10 thomas }
596 795b88cb 2023-07-10 thomas }
597 795b88cb 2023-07-10 thomas
598 795b88cb 2023-07-10 thomas *author = strdup(got_author);
599 795b88cb 2023-07-10 thomas if (*author == NULL)
600 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
601 795b88cb 2023-07-10 thomas
602 795b88cb 2023-07-10 thomas err = valid_author(*author);
603 795b88cb 2023-07-10 thomas if (err) {
604 795b88cb 2023-07-10 thomas free(*author);
605 795b88cb 2023-07-10 thomas *author = NULL;
606 795b88cb 2023-07-10 thomas }
607 795b88cb 2023-07-10 thomas return err;
608 795b88cb 2023-07-10 thomas }
609 795b88cb 2023-07-10 thomas
610 795b88cb 2023-07-10 thomas static const struct got_error *
611 795b88cb 2023-07-10 thomas get_allowed_signers(char **allowed_signers, struct got_repository *repo,
612 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
613 795b88cb 2023-07-10 thomas {
614 795b88cb 2023-07-10 thomas const char *got_allowed_signers = NULL;
615 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
616 795b88cb 2023-07-10 thomas
617 795b88cb 2023-07-10 thomas *allowed_signers = NULL;
618 795b88cb 2023-07-10 thomas
619 795b88cb 2023-07-10 thomas if (worktree)
620 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
621 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
622 795b88cb 2023-07-10 thomas
623 795b88cb 2023-07-10 thomas /*
624 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
625 795b88cb 2023-07-10 thomas * significant to least significant:
626 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
627 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
628 795b88cb 2023-07-10 thomas */
629 795b88cb 2023-07-10 thomas
630 795b88cb 2023-07-10 thomas if (worktree_conf)
631 795b88cb 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
632 795b88cb 2023-07-10 thomas worktree_conf);
633 795b88cb 2023-07-10 thomas if (got_allowed_signers == NULL)
634 795b88cb 2023-07-10 thomas got_allowed_signers = got_gotconfig_get_allowed_signers_file(
635 795b88cb 2023-07-10 thomas repo_conf);
636 795b88cb 2023-07-10 thomas
637 795b88cb 2023-07-10 thomas if (got_allowed_signers) {
638 795b88cb 2023-07-10 thomas *allowed_signers = strdup(got_allowed_signers);
639 795b88cb 2023-07-10 thomas if (*allowed_signers == NULL)
640 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
641 795b88cb 2023-07-10 thomas }
642 795b88cb 2023-07-10 thomas return NULL;
643 795b88cb 2023-07-10 thomas }
644 795b88cb 2023-07-10 thomas
645 795b88cb 2023-07-10 thomas static const struct got_error *
646 795b88cb 2023-07-10 thomas get_revoked_signers(char **revoked_signers, struct got_repository *repo,
647 795b88cb 2023-07-10 thomas struct got_worktree *worktree)
648 795b88cb 2023-07-10 thomas {
649 795b88cb 2023-07-10 thomas const char *got_revoked_signers = NULL;
650 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
651 795b88cb 2023-07-10 thomas
652 795b88cb 2023-07-10 thomas *revoked_signers = NULL;
653 795b88cb 2023-07-10 thomas
654 795b88cb 2023-07-10 thomas if (worktree)
655 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
656 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
657 795b88cb 2023-07-10 thomas
658 795b88cb 2023-07-10 thomas /*
659 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
660 795b88cb 2023-07-10 thomas * significant to least significant:
661 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
662 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
663 795b88cb 2023-07-10 thomas */
664 795b88cb 2023-07-10 thomas
665 795b88cb 2023-07-10 thomas if (worktree_conf)
666 795b88cb 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
667 795b88cb 2023-07-10 thomas worktree_conf);
668 795b88cb 2023-07-10 thomas if (got_revoked_signers == NULL)
669 795b88cb 2023-07-10 thomas got_revoked_signers = got_gotconfig_get_revoked_signers_file(
670 795b88cb 2023-07-10 thomas repo_conf);
671 795b88cb 2023-07-10 thomas
672 795b88cb 2023-07-10 thomas if (got_revoked_signers) {
673 795b88cb 2023-07-10 thomas *revoked_signers = strdup(got_revoked_signers);
674 795b88cb 2023-07-10 thomas if (*revoked_signers == NULL)
675 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
676 795b88cb 2023-07-10 thomas }
677 795b88cb 2023-07-10 thomas return NULL;
678 795b88cb 2023-07-10 thomas }
679 795b88cb 2023-07-10 thomas
680 795b88cb 2023-07-10 thomas static const char *
681 795b88cb 2023-07-10 thomas get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
682 795b88cb 2023-07-10 thomas {
683 795b88cb 2023-07-10 thomas const char *got_signer_id = NULL;
684 795b88cb 2023-07-10 thomas const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
685 795b88cb 2023-07-10 thomas
686 795b88cb 2023-07-10 thomas if (worktree)
687 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
688 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
689 795b88cb 2023-07-10 thomas
690 795b88cb 2023-07-10 thomas /*
691 795b88cb 2023-07-10 thomas * Priority of potential author information sources, from most
692 795b88cb 2023-07-10 thomas * significant to least significant:
693 795b88cb 2023-07-10 thomas * 1) work tree's .got/got.conf file
694 795b88cb 2023-07-10 thomas * 2) repository's got.conf file
695 795b88cb 2023-07-10 thomas */
696 795b88cb 2023-07-10 thomas
697 795b88cb 2023-07-10 thomas if (worktree_conf)
698 795b88cb 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
699 795b88cb 2023-07-10 thomas if (got_signer_id == NULL)
700 795b88cb 2023-07-10 thomas got_signer_id = got_gotconfig_get_signer_id(repo_conf);
701 795b88cb 2023-07-10 thomas
702 795b88cb 2023-07-10 thomas return got_signer_id;
703 795b88cb 2023-07-10 thomas }
704 795b88cb 2023-07-10 thomas
705 795b88cb 2023-07-10 thomas static const struct got_error *
706 795b88cb 2023-07-10 thomas get_gitconfig_path(char **gitconfig_path)
707 795b88cb 2023-07-10 thomas {
708 795b88cb 2023-07-10 thomas const char *homedir = getenv("HOME");
709 795b88cb 2023-07-10 thomas
710 795b88cb 2023-07-10 thomas *gitconfig_path = NULL;
711 795b88cb 2023-07-10 thomas if (homedir) {
712 795b88cb 2023-07-10 thomas if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
713 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
714 795b88cb 2023-07-10 thomas
715 795b88cb 2023-07-10 thomas }
716 795b88cb 2023-07-10 thomas return NULL;
717 795b88cb 2023-07-10 thomas }
718 795b88cb 2023-07-10 thomas
719 795b88cb 2023-07-10 thomas static const struct got_error *
720 795b88cb 2023-07-10 thomas cmd_import(int argc, char *argv[])
721 795b88cb 2023-07-10 thomas {
722 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
723 795b88cb 2023-07-10 thomas char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
724 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
725 795b88cb 2023-07-10 thomas const char *branch_name = NULL;
726 795b88cb 2023-07-10 thomas char *id_str = NULL, *logmsg_path = NULL;
727 795b88cb 2023-07-10 thomas char refname[PATH_MAX] = "refs/heads/";
728 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
729 795b88cb 2023-07-10 thomas struct got_reference *branch_ref = NULL, *head_ref = NULL;
730 795b88cb 2023-07-10 thomas struct got_object_id *new_commit_id = NULL;
731 795b88cb 2023-07-10 thomas int ch, n = 0;
732 795b88cb 2023-07-10 thomas struct got_pathlist_head ignores;
733 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
734 795b88cb 2023-07-10 thomas int preserve_logmsg = 0;
735 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
736 795b88cb 2023-07-10 thomas
737 795b88cb 2023-07-10 thomas TAILQ_INIT(&ignores);
738 795b88cb 2023-07-10 thomas
739 795b88cb 2023-07-10 thomas #ifndef PROFILE
740 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
741 795b88cb 2023-07-10 thomas "unveil",
742 795b88cb 2023-07-10 thomas NULL) == -1)
743 795b88cb 2023-07-10 thomas err(1, "pledge");
744 795b88cb 2023-07-10 thomas #endif
745 795b88cb 2023-07-10 thomas
746 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
747 795b88cb 2023-07-10 thomas switch (ch) {
748 795b88cb 2023-07-10 thomas case 'b':
749 795b88cb 2023-07-10 thomas branch_name = optarg;
750 795b88cb 2023-07-10 thomas break;
751 795b88cb 2023-07-10 thomas case 'I':
752 795b88cb 2023-07-10 thomas if (optarg[0] == '\0')
753 795b88cb 2023-07-10 thomas break;
754 795b88cb 2023-07-10 thomas error = got_pathlist_insert(&pe, &ignores, optarg,
755 795b88cb 2023-07-10 thomas NULL);
756 795b88cb 2023-07-10 thomas if (error)
757 795b88cb 2023-07-10 thomas goto done;
758 795b88cb 2023-07-10 thomas break;
759 795b88cb 2023-07-10 thomas case 'm':
760 795b88cb 2023-07-10 thomas logmsg = strdup(optarg);
761 795b88cb 2023-07-10 thomas if (logmsg == NULL) {
762 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
763 795b88cb 2023-07-10 thomas goto done;
764 795b88cb 2023-07-10 thomas }
765 795b88cb 2023-07-10 thomas break;
766 795b88cb 2023-07-10 thomas case 'r':
767 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
768 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
769 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
770 795b88cb 2023-07-10 thomas optarg);
771 795b88cb 2023-07-10 thomas goto done;
772 795b88cb 2023-07-10 thomas }
773 795b88cb 2023-07-10 thomas break;
774 795b88cb 2023-07-10 thomas default:
775 795b88cb 2023-07-10 thomas usage_import();
776 795b88cb 2023-07-10 thomas /* NOTREACHED */
777 795b88cb 2023-07-10 thomas }
778 795b88cb 2023-07-10 thomas }
779 795b88cb 2023-07-10 thomas
780 795b88cb 2023-07-10 thomas argc -= optind;
781 795b88cb 2023-07-10 thomas argv += optind;
782 795b88cb 2023-07-10 thomas
783 795b88cb 2023-07-10 thomas if (argc != 1)
784 795b88cb 2023-07-10 thomas usage_import();
785 795b88cb 2023-07-10 thomas
786 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
787 795b88cb 2023-07-10 thomas repo_path = getcwd(NULL, 0);
788 795b88cb 2023-07-10 thomas if (repo_path == NULL)
789 795b88cb 2023-07-10 thomas return got_error_from_errno("getcwd");
790 795b88cb 2023-07-10 thomas }
791 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
792 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
793 795b88cb 2023-07-10 thomas if (error)
794 795b88cb 2023-07-10 thomas goto done;
795 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
796 795b88cb 2023-07-10 thomas if (error != NULL)
797 795b88cb 2023-07-10 thomas goto done;
798 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
799 795b88cb 2023-07-10 thomas if (error)
800 795b88cb 2023-07-10 thomas goto done;
801 795b88cb 2023-07-10 thomas
802 795b88cb 2023-07-10 thomas error = get_author(&author, repo, NULL);
803 795b88cb 2023-07-10 thomas if (error)
804 795b88cb 2023-07-10 thomas return error;
805 795b88cb 2023-07-10 thomas
806 795b88cb 2023-07-10 thomas /*
807 795b88cb 2023-07-10 thomas * Don't let the user create a branch name with a leading '-'.
808 795b88cb 2023-07-10 thomas * While technically a valid reference name, this case is usually
809 795b88cb 2023-07-10 thomas * an unintended typo.
810 795b88cb 2023-07-10 thomas */
811 795b88cb 2023-07-10 thomas if (branch_name && branch_name[0] == '-')
812 795b88cb 2023-07-10 thomas return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
813 795b88cb 2023-07-10 thomas
814 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
815 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_REF)
816 795b88cb 2023-07-10 thomas goto done;
817 795b88cb 2023-07-10 thomas
818 795b88cb 2023-07-10 thomas if (branch_name)
819 795b88cb 2023-07-10 thomas n = strlcat(refname, branch_name, sizeof(refname));
820 795b88cb 2023-07-10 thomas else if (head_ref && got_ref_is_symbolic(head_ref))
821 795b88cb 2023-07-10 thomas n = strlcpy(refname, got_ref_get_symref_target(head_ref),
822 795b88cb 2023-07-10 thomas sizeof(refname));
823 795b88cb 2023-07-10 thomas else
824 795b88cb 2023-07-10 thomas n = strlcat(refname, "main", sizeof(refname));
825 795b88cb 2023-07-10 thomas if (n >= sizeof(refname)) {
826 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_NO_SPACE);
827 795b88cb 2023-07-10 thomas goto done;
828 795b88cb 2023-07-10 thomas }
829 795b88cb 2023-07-10 thomas
830 795b88cb 2023-07-10 thomas error = got_ref_open(&branch_ref, repo, refname, 0);
831 795b88cb 2023-07-10 thomas if (error) {
832 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
833 795b88cb 2023-07-10 thomas goto done;
834 795b88cb 2023-07-10 thomas } else {
835 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
836 795b88cb 2023-07-10 thomas "import target branch already exists");
837 795b88cb 2023-07-10 thomas goto done;
838 795b88cb 2023-07-10 thomas }
839 795b88cb 2023-07-10 thomas
840 795b88cb 2023-07-10 thomas path_dir = realpath(argv[0], NULL);
841 795b88cb 2023-07-10 thomas if (path_dir == NULL) {
842 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
843 795b88cb 2023-07-10 thomas goto done;
844 795b88cb 2023-07-10 thomas }
845 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(path_dir);
846 795b88cb 2023-07-10 thomas
847 795b88cb 2023-07-10 thomas /*
848 795b88cb 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
849 795b88cb 2023-07-10 thomas * to apply unveil after the log message has been written.
850 795b88cb 2023-07-10 thomas */
851 795b88cb 2023-07-10 thomas if (logmsg == NULL || *logmsg == '\0') {
852 795b88cb 2023-07-10 thomas error = get_editor(&editor);
853 795b88cb 2023-07-10 thomas if (error)
854 795b88cb 2023-07-10 thomas goto done;
855 795b88cb 2023-07-10 thomas free(logmsg);
856 795b88cb 2023-07-10 thomas error = collect_import_msg(&logmsg, &logmsg_path, editor,
857 795b88cb 2023-07-10 thomas path_dir, refname);
858 795b88cb 2023-07-10 thomas if (error) {
859 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
860 795b88cb 2023-07-10 thomas logmsg_path != NULL)
861 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
862 795b88cb 2023-07-10 thomas goto done;
863 795b88cb 2023-07-10 thomas }
864 795b88cb 2023-07-10 thomas }
865 795b88cb 2023-07-10 thomas
866 795b88cb 2023-07-10 thomas if (unveil(path_dir, "r") != 0) {
867 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unveil", path_dir);
868 795b88cb 2023-07-10 thomas if (logmsg_path)
869 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
870 795b88cb 2023-07-10 thomas goto done;
871 795b88cb 2023-07-10 thomas }
872 795b88cb 2023-07-10 thomas
873 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
874 795b88cb 2023-07-10 thomas if (error) {
875 795b88cb 2023-07-10 thomas if (logmsg_path)
876 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
877 795b88cb 2023-07-10 thomas goto done;
878 795b88cb 2023-07-10 thomas }
879 795b88cb 2023-07-10 thomas
880 795b88cb 2023-07-10 thomas error = got_repo_import(&new_commit_id, path_dir, logmsg,
881 795b88cb 2023-07-10 thomas author, &ignores, repo, import_progress, NULL);
882 795b88cb 2023-07-10 thomas if (error) {
883 795b88cb 2023-07-10 thomas if (logmsg_path)
884 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
885 795b88cb 2023-07-10 thomas goto done;
886 795b88cb 2023-07-10 thomas }
887 795b88cb 2023-07-10 thomas
888 795b88cb 2023-07-10 thomas error = got_ref_alloc(&branch_ref, refname, new_commit_id);
889 795b88cb 2023-07-10 thomas if (error) {
890 795b88cb 2023-07-10 thomas if (logmsg_path)
891 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
892 795b88cb 2023-07-10 thomas goto done;
893 795b88cb 2023-07-10 thomas }
894 795b88cb 2023-07-10 thomas
895 795b88cb 2023-07-10 thomas error = got_ref_write(branch_ref, repo);
896 795b88cb 2023-07-10 thomas if (error) {
897 795b88cb 2023-07-10 thomas if (logmsg_path)
898 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
899 795b88cb 2023-07-10 thomas goto done;
900 795b88cb 2023-07-10 thomas }
901 795b88cb 2023-07-10 thomas
902 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, new_commit_id);
903 795b88cb 2023-07-10 thomas if (error) {
904 795b88cb 2023-07-10 thomas if (logmsg_path)
905 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
906 795b88cb 2023-07-10 thomas goto done;
907 795b88cb 2023-07-10 thomas }
908 795b88cb 2023-07-10 thomas
909 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
910 795b88cb 2023-07-10 thomas if (error) {
911 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF) {
912 795b88cb 2023-07-10 thomas if (logmsg_path)
913 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
914 795b88cb 2023-07-10 thomas goto done;
915 795b88cb 2023-07-10 thomas }
916 795b88cb 2023-07-10 thomas
917 795b88cb 2023-07-10 thomas error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
918 795b88cb 2023-07-10 thomas branch_ref);
919 795b88cb 2023-07-10 thomas if (error) {
920 795b88cb 2023-07-10 thomas if (logmsg_path)
921 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
922 795b88cb 2023-07-10 thomas goto done;
923 795b88cb 2023-07-10 thomas }
924 795b88cb 2023-07-10 thomas
925 795b88cb 2023-07-10 thomas error = got_ref_write(head_ref, repo);
926 795b88cb 2023-07-10 thomas if (error) {
927 795b88cb 2023-07-10 thomas if (logmsg_path)
928 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
929 795b88cb 2023-07-10 thomas goto done;
930 795b88cb 2023-07-10 thomas }
931 795b88cb 2023-07-10 thomas }
932 795b88cb 2023-07-10 thomas
933 795b88cb 2023-07-10 thomas printf("Created branch %s with commit %s\n",
934 795b88cb 2023-07-10 thomas got_ref_get_name(branch_ref), id_str);
935 795b88cb 2023-07-10 thomas done:
936 795b88cb 2023-07-10 thomas if (pack_fds) {
937 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
938 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
939 795b88cb 2023-07-10 thomas if (error == NULL)
940 795b88cb 2023-07-10 thomas error = pack_err;
941 795b88cb 2023-07-10 thomas }
942 795b88cb 2023-07-10 thomas if (repo) {
943 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
944 795b88cb 2023-07-10 thomas if (error == NULL)
945 795b88cb 2023-07-10 thomas error = close_err;
946 795b88cb 2023-07-10 thomas }
947 795b88cb 2023-07-10 thomas if (preserve_logmsg) {
948 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
949 795b88cb 2023-07-10 thomas getprogname(), logmsg_path);
950 795b88cb 2023-07-10 thomas } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
951 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", logmsg_path);
952 795b88cb 2023-07-10 thomas free(logmsg);
953 795b88cb 2023-07-10 thomas free(logmsg_path);
954 795b88cb 2023-07-10 thomas free(repo_path);
955 795b88cb 2023-07-10 thomas free(editor);
956 795b88cb 2023-07-10 thomas free(new_commit_id);
957 795b88cb 2023-07-10 thomas free(id_str);
958 795b88cb 2023-07-10 thomas free(author);
959 795b88cb 2023-07-10 thomas free(gitconfig_path);
960 795b88cb 2023-07-10 thomas if (branch_ref)
961 795b88cb 2023-07-10 thomas got_ref_close(branch_ref);
962 795b88cb 2023-07-10 thomas if (head_ref)
963 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
964 795b88cb 2023-07-10 thomas return error;
965 795b88cb 2023-07-10 thomas }
966 795b88cb 2023-07-10 thomas
967 795b88cb 2023-07-10 thomas __dead static void
968 795b88cb 2023-07-10 thomas usage_clone(void)
969 795b88cb 2023-07-10 thomas {
970 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
971 795b88cb 2023-07-10 thomas "repository-URL [directory]\n", getprogname());
972 795b88cb 2023-07-10 thomas exit(1);
973 795b88cb 2023-07-10 thomas }
974 795b88cb 2023-07-10 thomas
975 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg {
976 795b88cb 2023-07-10 thomas char last_scaled_size[FMT_SCALED_STRSIZE];
977 795b88cb 2023-07-10 thomas int last_p_indexed;
978 795b88cb 2023-07-10 thomas int last_p_resolved;
979 795b88cb 2023-07-10 thomas int verbosity;
980 795b88cb 2023-07-10 thomas
981 795b88cb 2023-07-10 thomas struct got_repository *repo;
982 795b88cb 2023-07-10 thomas
983 795b88cb 2023-07-10 thomas int create_configs;
984 795b88cb 2023-07-10 thomas int configs_created;
985 795b88cb 2023-07-10 thomas struct {
986 795b88cb 2023-07-10 thomas struct got_pathlist_head *symrefs;
987 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches;
988 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs;
989 795b88cb 2023-07-10 thomas const char *proto;
990 795b88cb 2023-07-10 thomas const char *host;
991 795b88cb 2023-07-10 thomas const char *port;
992 795b88cb 2023-07-10 thomas const char *remote_repo_path;
993 795b88cb 2023-07-10 thomas const char *git_url;
994 795b88cb 2023-07-10 thomas int fetch_all_branches;
995 795b88cb 2023-07-10 thomas int mirror_references;
996 795b88cb 2023-07-10 thomas } config_info;
997 795b88cb 2023-07-10 thomas };
998 795b88cb 2023-07-10 thomas
999 795b88cb 2023-07-10 thomas /* XXX forward declaration */
1000 795b88cb 2023-07-10 thomas static const struct got_error *
1001 795b88cb 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1002 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1003 795b88cb 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1004 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1005 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1006 795b88cb 2023-07-10 thomas
1007 795b88cb 2023-07-10 thomas static const struct got_error *
1008 795b88cb 2023-07-10 thomas fetch_progress(void *arg, const char *message, off_t packfile_size,
1009 795b88cb 2023-07-10 thomas int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1010 795b88cb 2023-07-10 thomas {
1011 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1012 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg *a = arg;
1013 795b88cb 2023-07-10 thomas char scaled_size[FMT_SCALED_STRSIZE];
1014 795b88cb 2023-07-10 thomas int p_indexed, p_resolved;
1015 795b88cb 2023-07-10 thomas int print_size = 0, print_indexed = 0, print_resolved = 0;
1016 795b88cb 2023-07-10 thomas
1017 795b88cb 2023-07-10 thomas /*
1018 795b88cb 2023-07-10 thomas * In order to allow a failed clone to be resumed with 'got fetch'
1019 795b88cb 2023-07-10 thomas * we try to create configuration files as soon as possible.
1020 795b88cb 2023-07-10 thomas * Once the server has sent information about its default branch
1021 795b88cb 2023-07-10 thomas * we have all required information.
1022 795b88cb 2023-07-10 thomas */
1023 795b88cb 2023-07-10 thomas if (a->create_configs && !a->configs_created &&
1024 795b88cb 2023-07-10 thomas !TAILQ_EMPTY(a->config_info.symrefs)) {
1025 795b88cb 2023-07-10 thomas err = create_config_files(a->config_info.proto,
1026 795b88cb 2023-07-10 thomas a->config_info.host, a->config_info.port,
1027 795b88cb 2023-07-10 thomas a->config_info.remote_repo_path,
1028 795b88cb 2023-07-10 thomas a->config_info.git_url,
1029 795b88cb 2023-07-10 thomas a->config_info.fetch_all_branches,
1030 795b88cb 2023-07-10 thomas a->config_info.mirror_references,
1031 795b88cb 2023-07-10 thomas a->config_info.symrefs,
1032 795b88cb 2023-07-10 thomas a->config_info.wanted_branches,
1033 795b88cb 2023-07-10 thomas a->config_info.wanted_refs, a->repo);
1034 795b88cb 2023-07-10 thomas if (err)
1035 795b88cb 2023-07-10 thomas return err;
1036 795b88cb 2023-07-10 thomas a->configs_created = 1;
1037 795b88cb 2023-07-10 thomas }
1038 795b88cb 2023-07-10 thomas
1039 795b88cb 2023-07-10 thomas if (a->verbosity < 0)
1040 795b88cb 2023-07-10 thomas return NULL;
1041 795b88cb 2023-07-10 thomas
1042 795b88cb 2023-07-10 thomas if (message && message[0] != '\0') {
1043 795b88cb 2023-07-10 thomas printf("\rserver: %s", message);
1044 795b88cb 2023-07-10 thomas fflush(stdout);
1045 795b88cb 2023-07-10 thomas return NULL;
1046 795b88cb 2023-07-10 thomas }
1047 795b88cb 2023-07-10 thomas
1048 795b88cb 2023-07-10 thomas if (packfile_size > 0 || nobj_indexed > 0) {
1049 795b88cb 2023-07-10 thomas if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1050 795b88cb 2023-07-10 thomas (a->last_scaled_size[0] == '\0' ||
1051 795b88cb 2023-07-10 thomas strcmp(scaled_size, a->last_scaled_size)) != 0) {
1052 795b88cb 2023-07-10 thomas print_size = 1;
1053 795b88cb 2023-07-10 thomas if (strlcpy(a->last_scaled_size, scaled_size,
1054 795b88cb 2023-07-10 thomas FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1055 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
1056 795b88cb 2023-07-10 thomas }
1057 795b88cb 2023-07-10 thomas if (nobj_indexed > 0) {
1058 795b88cb 2023-07-10 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
1059 795b88cb 2023-07-10 thomas if (p_indexed != a->last_p_indexed) {
1060 795b88cb 2023-07-10 thomas a->last_p_indexed = p_indexed;
1061 795b88cb 2023-07-10 thomas print_indexed = 1;
1062 795b88cb 2023-07-10 thomas print_size = 1;
1063 795b88cb 2023-07-10 thomas }
1064 795b88cb 2023-07-10 thomas }
1065 795b88cb 2023-07-10 thomas if (nobj_resolved > 0) {
1066 795b88cb 2023-07-10 thomas p_resolved = (nobj_resolved * 100) /
1067 795b88cb 2023-07-10 thomas (nobj_total - nobj_loose);
1068 795b88cb 2023-07-10 thomas if (p_resolved != a->last_p_resolved) {
1069 795b88cb 2023-07-10 thomas a->last_p_resolved = p_resolved;
1070 795b88cb 2023-07-10 thomas print_resolved = 1;
1071 795b88cb 2023-07-10 thomas print_indexed = 1;
1072 795b88cb 2023-07-10 thomas print_size = 1;
1073 795b88cb 2023-07-10 thomas }
1074 795b88cb 2023-07-10 thomas }
1075 795b88cb 2023-07-10 thomas
1076 795b88cb 2023-07-10 thomas }
1077 795b88cb 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1078 795b88cb 2023-07-10 thomas printf("\r");
1079 795b88cb 2023-07-10 thomas if (print_size)
1080 795b88cb 2023-07-10 thomas printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1081 795b88cb 2023-07-10 thomas if (print_indexed)
1082 795b88cb 2023-07-10 thomas printf("; indexing %d%%", p_indexed);
1083 795b88cb 2023-07-10 thomas if (print_resolved)
1084 795b88cb 2023-07-10 thomas printf("; resolving deltas %d%%", p_resolved);
1085 795b88cb 2023-07-10 thomas if (print_size || print_indexed || print_resolved)
1086 795b88cb 2023-07-10 thomas fflush(stdout);
1087 795b88cb 2023-07-10 thomas
1088 795b88cb 2023-07-10 thomas return NULL;
1089 795b88cb 2023-07-10 thomas }
1090 795b88cb 2023-07-10 thomas
1091 795b88cb 2023-07-10 thomas static const struct got_error *
1092 795b88cb 2023-07-10 thomas create_symref(const char *refname, struct got_reference *target_ref,
1093 795b88cb 2023-07-10 thomas int verbosity, struct got_repository *repo)
1094 795b88cb 2023-07-10 thomas {
1095 795b88cb 2023-07-10 thomas const struct got_error *err;
1096 795b88cb 2023-07-10 thomas struct got_reference *head_symref;
1097 795b88cb 2023-07-10 thomas
1098 795b88cb 2023-07-10 thomas err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1099 795b88cb 2023-07-10 thomas if (err)
1100 795b88cb 2023-07-10 thomas return err;
1101 795b88cb 2023-07-10 thomas
1102 795b88cb 2023-07-10 thomas err = got_ref_write(head_symref, repo);
1103 795b88cb 2023-07-10 thomas if (err == NULL && verbosity > 0) {
1104 795b88cb 2023-07-10 thomas printf("Created reference %s: %s\n", GOT_REF_HEAD,
1105 795b88cb 2023-07-10 thomas got_ref_get_name(target_ref));
1106 795b88cb 2023-07-10 thomas }
1107 795b88cb 2023-07-10 thomas got_ref_close(head_symref);
1108 795b88cb 2023-07-10 thomas return err;
1109 795b88cb 2023-07-10 thomas }
1110 795b88cb 2023-07-10 thomas
1111 795b88cb 2023-07-10 thomas static const struct got_error *
1112 795b88cb 2023-07-10 thomas list_remote_refs(struct got_pathlist_head *symrefs,
1113 795b88cb 2023-07-10 thomas struct got_pathlist_head *refs)
1114 795b88cb 2023-07-10 thomas {
1115 795b88cb 2023-07-10 thomas const struct got_error *err;
1116 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1117 795b88cb 2023-07-10 thomas
1118 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1119 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1120 795b88cb 2023-07-10 thomas const char *targetref = pe->data;
1121 795b88cb 2023-07-10 thomas
1122 795b88cb 2023-07-10 thomas printf("%s: %s\n", refname, targetref);
1123 795b88cb 2023-07-10 thomas }
1124 795b88cb 2023-07-10 thomas
1125 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, refs, entry) {
1126 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1127 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
1128 795b88cb 2023-07-10 thomas char *id_str;
1129 795b88cb 2023-07-10 thomas
1130 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1131 795b88cb 2023-07-10 thomas if (err)
1132 795b88cb 2023-07-10 thomas return err;
1133 795b88cb 2023-07-10 thomas printf("%s: %s\n", refname, id_str);
1134 795b88cb 2023-07-10 thomas free(id_str);
1135 795b88cb 2023-07-10 thomas }
1136 795b88cb 2023-07-10 thomas
1137 795b88cb 2023-07-10 thomas return NULL;
1138 795b88cb 2023-07-10 thomas }
1139 795b88cb 2023-07-10 thomas
1140 795b88cb 2023-07-10 thomas static const struct got_error *
1141 795b88cb 2023-07-10 thomas create_ref(const char *refname, struct got_object_id *id,
1142 795b88cb 2023-07-10 thomas int verbosity, struct got_repository *repo)
1143 795b88cb 2023-07-10 thomas {
1144 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1145 795b88cb 2023-07-10 thomas struct got_reference *ref;
1146 795b88cb 2023-07-10 thomas char *id_str;
1147 795b88cb 2023-07-10 thomas
1148 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
1149 795b88cb 2023-07-10 thomas if (err)
1150 795b88cb 2023-07-10 thomas return err;
1151 795b88cb 2023-07-10 thomas
1152 795b88cb 2023-07-10 thomas err = got_ref_alloc(&ref, refname, id);
1153 795b88cb 2023-07-10 thomas if (err)
1154 795b88cb 2023-07-10 thomas goto done;
1155 795b88cb 2023-07-10 thomas
1156 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1157 795b88cb 2023-07-10 thomas got_ref_close(ref);
1158 795b88cb 2023-07-10 thomas
1159 795b88cb 2023-07-10 thomas if (err == NULL && verbosity >= 0)
1160 795b88cb 2023-07-10 thomas printf("Created reference %s: %s\n", refname, id_str);
1161 795b88cb 2023-07-10 thomas done:
1162 795b88cb 2023-07-10 thomas free(id_str);
1163 795b88cb 2023-07-10 thomas return err;
1164 795b88cb 2023-07-10 thomas }
1165 795b88cb 2023-07-10 thomas
1166 795b88cb 2023-07-10 thomas static int
1167 795b88cb 2023-07-10 thomas match_wanted_ref(const char *refname, const char *wanted_ref)
1168 795b88cb 2023-07-10 thomas {
1169 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) != 0)
1170 795b88cb 2023-07-10 thomas return 0;
1171 795b88cb 2023-07-10 thomas refname += 5;
1172 795b88cb 2023-07-10 thomas
1173 795b88cb 2023-07-10 thomas /*
1174 795b88cb 2023-07-10 thomas * Prevent fetching of references that won't make any
1175 795b88cb 2023-07-10 thomas * sense outside of the remote repository's context.
1176 795b88cb 2023-07-10 thomas */
1177 795b88cb 2023-07-10 thomas if (strncmp(refname, "got/", 4) == 0)
1178 795b88cb 2023-07-10 thomas return 0;
1179 795b88cb 2023-07-10 thomas if (strncmp(refname, "remotes/", 8) == 0)
1180 795b88cb 2023-07-10 thomas return 0;
1181 795b88cb 2023-07-10 thomas
1182 795b88cb 2023-07-10 thomas if (strncmp(wanted_ref, "refs/", 5) == 0)
1183 795b88cb 2023-07-10 thomas wanted_ref += 5;
1184 795b88cb 2023-07-10 thomas
1185 795b88cb 2023-07-10 thomas /* Allow prefix match. */
1186 795b88cb 2023-07-10 thomas if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1187 795b88cb 2023-07-10 thomas return 1;
1188 795b88cb 2023-07-10 thomas
1189 795b88cb 2023-07-10 thomas /* Allow exact match. */
1190 795b88cb 2023-07-10 thomas return (strcmp(refname, wanted_ref) == 0);
1191 795b88cb 2023-07-10 thomas }
1192 795b88cb 2023-07-10 thomas
1193 795b88cb 2023-07-10 thomas static int
1194 795b88cb 2023-07-10 thomas is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1195 795b88cb 2023-07-10 thomas {
1196 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1197 795b88cb 2023-07-10 thomas
1198 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1199 795b88cb 2023-07-10 thomas if (match_wanted_ref(refname, pe->path))
1200 795b88cb 2023-07-10 thomas return 1;
1201 795b88cb 2023-07-10 thomas }
1202 795b88cb 2023-07-10 thomas
1203 795b88cb 2023-07-10 thomas return 0;
1204 795b88cb 2023-07-10 thomas }
1205 795b88cb 2023-07-10 thomas
1206 795b88cb 2023-07-10 thomas static const struct got_error *
1207 795b88cb 2023-07-10 thomas create_wanted_ref(const char *refname, struct got_object_id *id,
1208 795b88cb 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1209 795b88cb 2023-07-10 thomas {
1210 795b88cb 2023-07-10 thomas const struct got_error *err;
1211 795b88cb 2023-07-10 thomas char *remote_refname;
1212 795b88cb 2023-07-10 thomas
1213 795b88cb 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1214 795b88cb 2023-07-10 thomas refname += 5;
1215 795b88cb 2023-07-10 thomas
1216 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1217 795b88cb 2023-07-10 thomas remote_repo_name, refname) == -1)
1218 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
1219 795b88cb 2023-07-10 thomas
1220 795b88cb 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1221 795b88cb 2023-07-10 thomas free(remote_refname);
1222 795b88cb 2023-07-10 thomas return err;
1223 795b88cb 2023-07-10 thomas }
1224 795b88cb 2023-07-10 thomas
1225 795b88cb 2023-07-10 thomas static const struct got_error *
1226 795b88cb 2023-07-10 thomas create_gotconfig(const char *proto, const char *host, const char *port,
1227 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *default_branch,
1228 795b88cb 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1229 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1230 795b88cb 2023-07-10 thomas struct got_repository *repo)
1231 795b88cb 2023-07-10 thomas {
1232 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1233 795b88cb 2023-07-10 thomas char *gotconfig_path = NULL;
1234 795b88cb 2023-07-10 thomas char *gotconfig = NULL;
1235 795b88cb 2023-07-10 thomas FILE *gotconfig_file = NULL;
1236 795b88cb 2023-07-10 thomas const char *branchname = NULL;
1237 795b88cb 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1238 795b88cb 2023-07-10 thomas ssize_t n;
1239 795b88cb 2023-07-10 thomas
1240 795b88cb 2023-07-10 thomas if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1241 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1242 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1243 795b88cb 2023-07-10 thomas char *s;
1244 795b88cb 2023-07-10 thomas branchname = pe->path;
1245 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1246 795b88cb 2023-07-10 thomas branchname += 11;
1247 795b88cb 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1248 795b88cb 2023-07-10 thomas branches ? branches : "", branchname) == -1) {
1249 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1250 795b88cb 2023-07-10 thomas goto done;
1251 795b88cb 2023-07-10 thomas }
1252 795b88cb 2023-07-10 thomas free(branches);
1253 795b88cb 2023-07-10 thomas branches = s;
1254 795b88cb 2023-07-10 thomas }
1255 795b88cb 2023-07-10 thomas } else if (!fetch_all_branches && default_branch) {
1256 795b88cb 2023-07-10 thomas branchname = default_branch;
1257 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1258 795b88cb 2023-07-10 thomas branchname += 11;
1259 795b88cb 2023-07-10 thomas if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1260 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1261 795b88cb 2023-07-10 thomas goto done;
1262 795b88cb 2023-07-10 thomas }
1263 795b88cb 2023-07-10 thomas }
1264 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1265 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1266 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1267 795b88cb 2023-07-10 thomas char *s;
1268 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1269 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1270 795b88cb 2023-07-10 thomas branchname += 5;
1271 795b88cb 2023-07-10 thomas if (asprintf(&s, "%s\"%s\" ",
1272 795b88cb 2023-07-10 thomas refs ? refs : "", refname) == -1) {
1273 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1274 795b88cb 2023-07-10 thomas goto done;
1275 795b88cb 2023-07-10 thomas }
1276 795b88cb 2023-07-10 thomas free(refs);
1277 795b88cb 2023-07-10 thomas refs = s;
1278 795b88cb 2023-07-10 thomas }
1279 795b88cb 2023-07-10 thomas }
1280 795b88cb 2023-07-10 thomas
1281 795b88cb 2023-07-10 thomas /* Create got.conf(5). */
1282 795b88cb 2023-07-10 thomas gotconfig_path = got_repo_get_path_gotconfig(repo);
1283 795b88cb 2023-07-10 thomas if (gotconfig_path == NULL) {
1284 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gotconfig");
1285 795b88cb 2023-07-10 thomas goto done;
1286 795b88cb 2023-07-10 thomas }
1287 795b88cb 2023-07-10 thomas gotconfig_file = fopen(gotconfig_path, "ae");
1288 795b88cb 2023-07-10 thomas if (gotconfig_file == NULL) {
1289 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fopen", gotconfig_path);
1290 795b88cb 2023-07-10 thomas goto done;
1291 795b88cb 2023-07-10 thomas }
1292 795b88cb 2023-07-10 thomas if (asprintf(&gotconfig,
1293 795b88cb 2023-07-10 thomas "remote \"%s\" {\n"
1294 795b88cb 2023-07-10 thomas "\tserver %s\n"
1295 795b88cb 2023-07-10 thomas "\tprotocol %s\n"
1296 795b88cb 2023-07-10 thomas "%s%s%s"
1297 795b88cb 2023-07-10 thomas "\trepository \"%s\"\n"
1298 795b88cb 2023-07-10 thomas "%s%s%s"
1299 795b88cb 2023-07-10 thomas "%s%s%s"
1300 795b88cb 2023-07-10 thomas "%s"
1301 795b88cb 2023-07-10 thomas "%s"
1302 795b88cb 2023-07-10 thomas "}\n",
1303 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1304 795b88cb 2023-07-10 thomas port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1305 795b88cb 2023-07-10 thomas remote_repo_path, branches ? "\tbranch { " : "",
1306 795b88cb 2023-07-10 thomas branches ? branches : "", branches ? "}\n" : "",
1307 795b88cb 2023-07-10 thomas refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1308 795b88cb 2023-07-10 thomas mirror_references ? "\tmirror_references yes\n" : "",
1309 795b88cb 2023-07-10 thomas fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1310 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1311 795b88cb 2023-07-10 thomas goto done;
1312 795b88cb 2023-07-10 thomas }
1313 795b88cb 2023-07-10 thomas n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1314 795b88cb 2023-07-10 thomas if (n != strlen(gotconfig)) {
1315 795b88cb 2023-07-10 thomas err = got_ferror(gotconfig_file, GOT_ERR_IO);
1316 795b88cb 2023-07-10 thomas goto done;
1317 795b88cb 2023-07-10 thomas }
1318 795b88cb 2023-07-10 thomas
1319 795b88cb 2023-07-10 thomas done:
1320 795b88cb 2023-07-10 thomas if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1321 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", gotconfig_path);
1322 795b88cb 2023-07-10 thomas free(gotconfig_path);
1323 795b88cb 2023-07-10 thomas free(branches);
1324 795b88cb 2023-07-10 thomas return err;
1325 795b88cb 2023-07-10 thomas }
1326 795b88cb 2023-07-10 thomas
1327 795b88cb 2023-07-10 thomas static const struct got_error *
1328 795b88cb 2023-07-10 thomas create_gitconfig(const char *git_url, const char *default_branch,
1329 795b88cb 2023-07-10 thomas int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1330 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, int mirror_references,
1331 795b88cb 2023-07-10 thomas struct got_repository *repo)
1332 795b88cb 2023-07-10 thomas {
1333 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1334 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL;
1335 795b88cb 2023-07-10 thomas char *gitconfig = NULL;
1336 795b88cb 2023-07-10 thomas FILE *gitconfig_file = NULL;
1337 795b88cb 2023-07-10 thomas char *branches = NULL, *refs = NULL;
1338 795b88cb 2023-07-10 thomas const char *branchname;
1339 795b88cb 2023-07-10 thomas ssize_t n;
1340 795b88cb 2023-07-10 thomas
1341 795b88cb 2023-07-10 thomas /* Create a config file Git can understand. */
1342 795b88cb 2023-07-10 thomas gitconfig_path = got_repo_get_path_gitconfig(repo);
1343 795b88cb 2023-07-10 thomas if (gitconfig_path == NULL) {
1344 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_repo_get_path_gitconfig");
1345 795b88cb 2023-07-10 thomas goto done;
1346 795b88cb 2023-07-10 thomas }
1347 795b88cb 2023-07-10 thomas gitconfig_file = fopen(gitconfig_path, "ae");
1348 795b88cb 2023-07-10 thomas if (gitconfig_file == NULL) {
1349 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fopen", gitconfig_path);
1350 795b88cb 2023-07-10 thomas goto done;
1351 795b88cb 2023-07-10 thomas }
1352 795b88cb 2023-07-10 thomas if (fetch_all_branches) {
1353 795b88cb 2023-07-10 thomas if (mirror_references) {
1354 795b88cb 2023-07-10 thomas if (asprintf(&branches,
1355 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1356 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1357 795b88cb 2023-07-10 thomas goto done;
1358 795b88cb 2023-07-10 thomas }
1359 795b88cb 2023-07-10 thomas } else if (asprintf(&branches,
1360 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1361 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1362 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1363 795b88cb 2023-07-10 thomas goto done;
1364 795b88cb 2023-07-10 thomas }
1365 795b88cb 2023-07-10 thomas } else if (!TAILQ_EMPTY(wanted_branches)) {
1366 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1367 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
1368 795b88cb 2023-07-10 thomas char *s;
1369 795b88cb 2023-07-10 thomas branchname = pe->path;
1370 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1371 795b88cb 2023-07-10 thomas branchname += 11;
1372 795b88cb 2023-07-10 thomas if (mirror_references) {
1373 795b88cb 2023-07-10 thomas if (asprintf(&s,
1374 795b88cb 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1375 795b88cb 2023-07-10 thomas branches ? branches : "",
1376 795b88cb 2023-07-10 thomas branchname, branchname) == -1) {
1377 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1378 795b88cb 2023-07-10 thomas goto done;
1379 795b88cb 2023-07-10 thomas }
1380 795b88cb 2023-07-10 thomas } else if (asprintf(&s,
1381 795b88cb 2023-07-10 thomas "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1382 795b88cb 2023-07-10 thomas branches ? branches : "",
1383 795b88cb 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1384 795b88cb 2023-07-10 thomas branchname) == -1) {
1385 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1386 795b88cb 2023-07-10 thomas goto done;
1387 795b88cb 2023-07-10 thomas }
1388 795b88cb 2023-07-10 thomas free(branches);
1389 795b88cb 2023-07-10 thomas branches = s;
1390 795b88cb 2023-07-10 thomas }
1391 795b88cb 2023-07-10 thomas } else {
1392 795b88cb 2023-07-10 thomas /*
1393 795b88cb 2023-07-10 thomas * If the server specified a default branch, use just that one.
1394 795b88cb 2023-07-10 thomas * Otherwise fall back to fetching all branches on next fetch.
1395 795b88cb 2023-07-10 thomas */
1396 795b88cb 2023-07-10 thomas if (default_branch) {
1397 795b88cb 2023-07-10 thomas branchname = default_branch;
1398 795b88cb 2023-07-10 thomas if (strncmp(branchname, "refs/heads/", 11) == 0)
1399 795b88cb 2023-07-10 thomas branchname += 11;
1400 795b88cb 2023-07-10 thomas } else
1401 795b88cb 2023-07-10 thomas branchname = "*"; /* fall back to all branches */
1402 795b88cb 2023-07-10 thomas if (mirror_references) {
1403 795b88cb 2023-07-10 thomas if (asprintf(&branches,
1404 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/heads/%s\n",
1405 795b88cb 2023-07-10 thomas branchname, branchname) == -1) {
1406 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1407 795b88cb 2023-07-10 thomas goto done;
1408 795b88cb 2023-07-10 thomas }
1409 795b88cb 2023-07-10 thomas } else if (asprintf(&branches,
1410 795b88cb 2023-07-10 thomas "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1411 795b88cb 2023-07-10 thomas branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1412 795b88cb 2023-07-10 thomas branchname) == -1) {
1413 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1414 795b88cb 2023-07-10 thomas goto done;
1415 795b88cb 2023-07-10 thomas }
1416 795b88cb 2023-07-10 thomas }
1417 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_refs)) {
1418 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1419 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
1420 795b88cb 2023-07-10 thomas char *s;
1421 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1422 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/", 5) == 0)
1423 795b88cb 2023-07-10 thomas refname += 5;
1424 795b88cb 2023-07-10 thomas if (mirror_references) {
1425 795b88cb 2023-07-10 thomas if (asprintf(&s,
1426 795b88cb 2023-07-10 thomas "%s\tfetch = refs/%s:refs/%s\n",
1427 795b88cb 2023-07-10 thomas refs ? refs : "", refname, refname) == -1) {
1428 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1429 795b88cb 2023-07-10 thomas goto done;
1430 795b88cb 2023-07-10 thomas }
1431 795b88cb 2023-07-10 thomas } else if (asprintf(&s,
1432 795b88cb 2023-07-10 thomas "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1433 795b88cb 2023-07-10 thomas refs ? refs : "",
1434 795b88cb 2023-07-10 thomas refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1435 795b88cb 2023-07-10 thomas refname) == -1) {
1436 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1437 795b88cb 2023-07-10 thomas goto done;
1438 795b88cb 2023-07-10 thomas }
1439 795b88cb 2023-07-10 thomas free(refs);
1440 795b88cb 2023-07-10 thomas refs = s;
1441 795b88cb 2023-07-10 thomas }
1442 795b88cb 2023-07-10 thomas }
1443 795b88cb 2023-07-10 thomas
1444 795b88cb 2023-07-10 thomas if (asprintf(&gitconfig,
1445 795b88cb 2023-07-10 thomas "[remote \"%s\"]\n"
1446 795b88cb 2023-07-10 thomas "\turl = %s\n"
1447 795b88cb 2023-07-10 thomas "%s"
1448 795b88cb 2023-07-10 thomas "%s"
1449 795b88cb 2023-07-10 thomas "\tfetch = refs/tags/*:refs/tags/*\n",
1450 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1451 795b88cb 2023-07-10 thomas refs ? refs : "") == -1) {
1452 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
1453 795b88cb 2023-07-10 thomas goto done;
1454 795b88cb 2023-07-10 thomas }
1455 795b88cb 2023-07-10 thomas n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1456 795b88cb 2023-07-10 thomas if (n != strlen(gitconfig)) {
1457 795b88cb 2023-07-10 thomas err = got_ferror(gitconfig_file, GOT_ERR_IO);
1458 795b88cb 2023-07-10 thomas goto done;
1459 795b88cb 2023-07-10 thomas }
1460 795b88cb 2023-07-10 thomas done:
1461 795b88cb 2023-07-10 thomas if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1462 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", gitconfig_path);
1463 795b88cb 2023-07-10 thomas free(gitconfig_path);
1464 795b88cb 2023-07-10 thomas free(branches);
1465 795b88cb 2023-07-10 thomas return err;
1466 795b88cb 2023-07-10 thomas }
1467 795b88cb 2023-07-10 thomas
1468 795b88cb 2023-07-10 thomas static const struct got_error *
1469 795b88cb 2023-07-10 thomas create_config_files(const char *proto, const char *host, const char *port,
1470 795b88cb 2023-07-10 thomas const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1471 795b88cb 2023-07-10 thomas int mirror_references, struct got_pathlist_head *symrefs,
1472 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_branches,
1473 795b88cb 2023-07-10 thomas struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1474 795b88cb 2023-07-10 thomas {
1475 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1476 795b88cb 2023-07-10 thomas const char *default_branch = NULL;
1477 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1478 795b88cb 2023-07-10 thomas
1479 795b88cb 2023-07-10 thomas /*
1480 795b88cb 2023-07-10 thomas * If we asked for a set of wanted branches then use the first
1481 795b88cb 2023-07-10 thomas * one of those.
1482 795b88cb 2023-07-10 thomas */
1483 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(wanted_branches)) {
1484 795b88cb 2023-07-10 thomas pe = TAILQ_FIRST(wanted_branches);
1485 795b88cb 2023-07-10 thomas default_branch = pe->path;
1486 795b88cb 2023-07-10 thomas } else {
1487 795b88cb 2023-07-10 thomas /* First HEAD ref listed by server is the default branch. */
1488 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, symrefs, entry) {
1489 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1490 795b88cb 2023-07-10 thomas const char *target = pe->data;
1491 795b88cb 2023-07-10 thomas
1492 795b88cb 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1493 795b88cb 2023-07-10 thomas continue;
1494 795b88cb 2023-07-10 thomas
1495 795b88cb 2023-07-10 thomas default_branch = target;
1496 795b88cb 2023-07-10 thomas break;
1497 795b88cb 2023-07-10 thomas }
1498 795b88cb 2023-07-10 thomas }
1499 795b88cb 2023-07-10 thomas
1500 795b88cb 2023-07-10 thomas /* Create got.conf(5). */
1501 795b88cb 2023-07-10 thomas err = create_gotconfig(proto, host, port, remote_repo_path,
1502 795b88cb 2023-07-10 thomas default_branch, fetch_all_branches, wanted_branches,
1503 795b88cb 2023-07-10 thomas wanted_refs, mirror_references, repo);
1504 795b88cb 2023-07-10 thomas if (err)
1505 795b88cb 2023-07-10 thomas return err;
1506 795b88cb 2023-07-10 thomas
1507 795b88cb 2023-07-10 thomas /* Create a config file Git can understand. */
1508 795b88cb 2023-07-10 thomas return create_gitconfig(git_url, default_branch, fetch_all_branches,
1509 795b88cb 2023-07-10 thomas wanted_branches, wanted_refs, mirror_references, repo);
1510 795b88cb 2023-07-10 thomas }
1511 795b88cb 2023-07-10 thomas
1512 795b88cb 2023-07-10 thomas static const struct got_error *
1513 795b88cb 2023-07-10 thomas cmd_clone(int argc, char *argv[])
1514 795b88cb 2023-07-10 thomas {
1515 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
1516 795b88cb 2023-07-10 thomas const char *uri, *dirname;
1517 795b88cb 2023-07-10 thomas char *proto, *host, *port, *repo_name, *server_path;
1518 795b88cb 2023-07-10 thomas char *default_destdir = NULL, *id_str = NULL;
1519 795b88cb 2023-07-10 thomas const char *repo_path;
1520 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
1521 795b88cb 2023-07-10 thomas struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1522 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
1523 795b88cb 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
1524 795b88cb 2023-07-10 thomas int ch, fetchfd = -1, fetchstatus;
1525 795b88cb 2023-07-10 thomas pid_t fetchpid = -1;
1526 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg fpa;
1527 795b88cb 2023-07-10 thomas char *git_url = NULL;
1528 795b88cb 2023-07-10 thomas int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1529 795b88cb 2023-07-10 thomas int bflag = 0, list_refs_only = 0;
1530 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
1531 795b88cb 2023-07-10 thomas
1532 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
1533 795b88cb 2023-07-10 thomas TAILQ_INIT(&symrefs);
1534 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
1535 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
1536 795b88cb 2023-07-10 thomas
1537 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1538 795b88cb 2023-07-10 thomas switch (ch) {
1539 795b88cb 2023-07-10 thomas case 'a':
1540 795b88cb 2023-07-10 thomas fetch_all_branches = 1;
1541 795b88cb 2023-07-10 thomas break;
1542 795b88cb 2023-07-10 thomas case 'b':
1543 795b88cb 2023-07-10 thomas error = got_pathlist_append(&wanted_branches,
1544 795b88cb 2023-07-10 thomas optarg, NULL);
1545 795b88cb 2023-07-10 thomas if (error)
1546 795b88cb 2023-07-10 thomas return error;
1547 795b88cb 2023-07-10 thomas bflag = 1;
1548 795b88cb 2023-07-10 thomas break;
1549 795b88cb 2023-07-10 thomas case 'l':
1550 795b88cb 2023-07-10 thomas list_refs_only = 1;
1551 795b88cb 2023-07-10 thomas break;
1552 795b88cb 2023-07-10 thomas case 'm':
1553 795b88cb 2023-07-10 thomas mirror_references = 1;
1554 795b88cb 2023-07-10 thomas break;
1555 795b88cb 2023-07-10 thomas case 'q':
1556 795b88cb 2023-07-10 thomas verbosity = -1;
1557 795b88cb 2023-07-10 thomas break;
1558 795b88cb 2023-07-10 thomas case 'R':
1559 795b88cb 2023-07-10 thomas error = got_pathlist_append(&wanted_refs,
1560 795b88cb 2023-07-10 thomas optarg, NULL);
1561 795b88cb 2023-07-10 thomas if (error)
1562 795b88cb 2023-07-10 thomas return error;
1563 795b88cb 2023-07-10 thomas break;
1564 795b88cb 2023-07-10 thomas case 'v':
1565 795b88cb 2023-07-10 thomas if (verbosity < 0)
1566 795b88cb 2023-07-10 thomas verbosity = 0;
1567 795b88cb 2023-07-10 thomas else if (verbosity < 3)
1568 795b88cb 2023-07-10 thomas verbosity++;
1569 795b88cb 2023-07-10 thomas break;
1570 795b88cb 2023-07-10 thomas default:
1571 795b88cb 2023-07-10 thomas usage_clone();
1572 795b88cb 2023-07-10 thomas break;
1573 795b88cb 2023-07-10 thomas }
1574 795b88cb 2023-07-10 thomas }
1575 795b88cb 2023-07-10 thomas argc -= optind;
1576 795b88cb 2023-07-10 thomas argv += optind;
1577 795b88cb 2023-07-10 thomas
1578 795b88cb 2023-07-10 thomas if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1579 795b88cb 2023-07-10 thomas option_conflict('a', 'b');
1580 795b88cb 2023-07-10 thomas if (list_refs_only) {
1581 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_branches))
1582 795b88cb 2023-07-10 thomas option_conflict('l', 'b');
1583 795b88cb 2023-07-10 thomas if (fetch_all_branches)
1584 795b88cb 2023-07-10 thomas option_conflict('l', 'a');
1585 795b88cb 2023-07-10 thomas if (mirror_references)
1586 795b88cb 2023-07-10 thomas option_conflict('l', 'm');
1587 795b88cb 2023-07-10 thomas if (!TAILQ_EMPTY(&wanted_refs))
1588 795b88cb 2023-07-10 thomas option_conflict('l', 'R');
1589 795b88cb 2023-07-10 thomas }
1590 795b88cb 2023-07-10 thomas
1591 795b88cb 2023-07-10 thomas uri = argv[0];
1592 795b88cb 2023-07-10 thomas
1593 795b88cb 2023-07-10 thomas if (argc == 1)
1594 795b88cb 2023-07-10 thomas dirname = NULL;
1595 795b88cb 2023-07-10 thomas else if (argc == 2)
1596 795b88cb 2023-07-10 thomas dirname = argv[1];
1597 795b88cb 2023-07-10 thomas else
1598 795b88cb 2023-07-10 thomas usage_clone();
1599 795b88cb 2023-07-10 thomas
1600 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1601 795b88cb 2023-07-10 thomas &repo_name, uri);
1602 795b88cb 2023-07-10 thomas if (error)
1603 795b88cb 2023-07-10 thomas goto done;
1604 795b88cb 2023-07-10 thomas
1605 795b88cb 2023-07-10 thomas if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1606 795b88cb 2023-07-10 thomas host, port ? ":" : "", port ? port : "",
1607 795b88cb 2023-07-10 thomas server_path[0] != '/' ? "/" : "", server_path) == -1) {
1608 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1609 795b88cb 2023-07-10 thomas goto done;
1610 795b88cb 2023-07-10 thomas }
1611 795b88cb 2023-07-10 thomas
1612 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
1613 795b88cb 2023-07-10 thomas #ifndef PROFILE
1614 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1615 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
1616 795b88cb 2023-07-10 thomas err(1, "pledge");
1617 795b88cb 2023-07-10 thomas #endif
1618 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
1619 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
1620 795b88cb 2023-07-10 thomas #ifndef PROFILE
1621 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1622 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
1623 795b88cb 2023-07-10 thomas err(1, "pledge");
1624 795b88cb 2023-07-10 thomas #endif
1625 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
1626 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
1627 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1628 795b88cb 2023-07-10 thomas goto done;
1629 795b88cb 2023-07-10 thomas } else {
1630 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1631 795b88cb 2023-07-10 thomas goto done;
1632 795b88cb 2023-07-10 thomas }
1633 795b88cb 2023-07-10 thomas if (dirname == NULL) {
1634 795b88cb 2023-07-10 thomas if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1635 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1636 795b88cb 2023-07-10 thomas goto done;
1637 795b88cb 2023-07-10 thomas }
1638 795b88cb 2023-07-10 thomas repo_path = default_destdir;
1639 795b88cb 2023-07-10 thomas } else
1640 795b88cb 2023-07-10 thomas repo_path = dirname;
1641 795b88cb 2023-07-10 thomas
1642 795b88cb 2023-07-10 thomas if (!list_refs_only) {
1643 795b88cb 2023-07-10 thomas error = got_path_mkdir(repo_path);
1644 795b88cb 2023-07-10 thomas if (error &&
1645 795b88cb 2023-07-10 thomas (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1646 795b88cb 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1647 795b88cb 2023-07-10 thomas goto done;
1648 795b88cb 2023-07-10 thomas if (!got_path_dir_is_empty(repo_path)) {
1649 795b88cb 2023-07-10 thomas error = got_error_path(repo_path,
1650 795b88cb 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
1651 795b88cb 2023-07-10 thomas goto done;
1652 795b88cb 2023-07-10 thomas }
1653 795b88cb 2023-07-10 thomas }
1654 795b88cb 2023-07-10 thomas
1655 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
1656 795b88cb 2023-07-10 thomas if (error)
1657 795b88cb 2023-07-10 thomas goto done;
1658 795b88cb 2023-07-10 thomas
1659 795b88cb 2023-07-10 thomas error = apply_unveil(repo_path, 0, NULL);
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 if (verbosity >= 0)
1664 795b88cb 2023-07-10 thomas printf("Connecting to %s\n", git_url);
1665 795b88cb 2023-07-10 thomas
1666 795b88cb 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1667 795b88cb 2023-07-10 thomas server_path, verbosity);
1668 795b88cb 2023-07-10 thomas if (error)
1669 795b88cb 2023-07-10 thomas goto done;
1670 795b88cb 2023-07-10 thomas
1671 795b88cb 2023-07-10 thomas if (!list_refs_only) {
1672 795b88cb 2023-07-10 thomas error = got_repo_init(repo_path, NULL);
1673 795b88cb 2023-07-10 thomas if (error)
1674 795b88cb 2023-07-10 thomas goto done;
1675 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
1676 795b88cb 2023-07-10 thomas if (error != NULL)
1677 795b88cb 2023-07-10 thomas goto done;
1678 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1679 795b88cb 2023-07-10 thomas if (error)
1680 795b88cb 2023-07-10 thomas goto done;
1681 795b88cb 2023-07-10 thomas }
1682 795b88cb 2023-07-10 thomas
1683 795b88cb 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
1684 795b88cb 2023-07-10 thomas fpa.last_p_indexed = -1;
1685 795b88cb 2023-07-10 thomas fpa.last_p_resolved = -1;
1686 795b88cb 2023-07-10 thomas fpa.verbosity = verbosity;
1687 795b88cb 2023-07-10 thomas fpa.create_configs = 1;
1688 795b88cb 2023-07-10 thomas fpa.configs_created = 0;
1689 795b88cb 2023-07-10 thomas fpa.repo = repo;
1690 795b88cb 2023-07-10 thomas fpa.config_info.symrefs = &symrefs;
1691 795b88cb 2023-07-10 thomas fpa.config_info.wanted_branches = &wanted_branches;
1692 795b88cb 2023-07-10 thomas fpa.config_info.wanted_refs = &wanted_refs;
1693 795b88cb 2023-07-10 thomas fpa.config_info.proto = proto;
1694 795b88cb 2023-07-10 thomas fpa.config_info.host = host;
1695 795b88cb 2023-07-10 thomas fpa.config_info.port = port;
1696 795b88cb 2023-07-10 thomas fpa.config_info.remote_repo_path = server_path;
1697 795b88cb 2023-07-10 thomas fpa.config_info.git_url = git_url;
1698 795b88cb 2023-07-10 thomas fpa.config_info.fetch_all_branches = fetch_all_branches;
1699 795b88cb 2023-07-10 thomas fpa.config_info.mirror_references = mirror_references;
1700 795b88cb 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1701 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1702 795b88cb 2023-07-10 thomas fetch_all_branches, &wanted_branches, &wanted_refs,
1703 795b88cb 2023-07-10 thomas list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1704 795b88cb 2023-07-10 thomas fetch_progress, &fpa);
1705 795b88cb 2023-07-10 thomas if (error)
1706 795b88cb 2023-07-10 thomas goto done;
1707 795b88cb 2023-07-10 thomas
1708 795b88cb 2023-07-10 thomas if (list_refs_only) {
1709 795b88cb 2023-07-10 thomas error = list_remote_refs(&symrefs, &refs);
1710 795b88cb 2023-07-10 thomas goto done;
1711 795b88cb 2023-07-10 thomas }
1712 795b88cb 2023-07-10 thomas
1713 795b88cb 2023-07-10 thomas if (pack_hash == NULL) {
1714 795b88cb 2023-07-10 thomas error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1715 795b88cb 2023-07-10 thomas "server sent an empty pack file");
1716 795b88cb 2023-07-10 thomas goto done;
1717 795b88cb 2023-07-10 thomas }
1718 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
1719 795b88cb 2023-07-10 thomas if (error)
1720 795b88cb 2023-07-10 thomas goto done;
1721 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1722 795b88cb 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
1723 795b88cb 2023-07-10 thomas free(id_str);
1724 795b88cb 2023-07-10 thomas
1725 795b88cb 2023-07-10 thomas /* Set up references provided with the pack file. */
1726 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
1727 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1728 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
1729 795b88cb 2023-07-10 thomas char *remote_refname;
1730 795b88cb 2023-07-10 thomas
1731 795b88cb 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname) &&
1732 795b88cb 2023-07-10 thomas !mirror_references) {
1733 795b88cb 2023-07-10 thomas error = create_wanted_ref(refname, id,
1734 795b88cb 2023-07-10 thomas GOT_FETCH_DEFAULT_REMOTE_NAME,
1735 795b88cb 2023-07-10 thomas verbosity - 1, repo);
1736 795b88cb 2023-07-10 thomas if (error)
1737 795b88cb 2023-07-10 thomas goto done;
1738 795b88cb 2023-07-10 thomas continue;
1739 795b88cb 2023-07-10 thomas }
1740 795b88cb 2023-07-10 thomas
1741 795b88cb 2023-07-10 thomas error = create_ref(refname, id, verbosity - 1, repo);
1742 795b88cb 2023-07-10 thomas if (error)
1743 795b88cb 2023-07-10 thomas goto done;
1744 795b88cb 2023-07-10 thomas
1745 795b88cb 2023-07-10 thomas if (mirror_references)
1746 795b88cb 2023-07-10 thomas continue;
1747 795b88cb 2023-07-10 thomas
1748 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", refname, 11) != 0)
1749 795b88cb 2023-07-10 thomas continue;
1750 795b88cb 2023-07-10 thomas
1751 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname,
1752 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1753 795b88cb 2023-07-10 thomas refname + 11) == -1) {
1754 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1755 795b88cb 2023-07-10 thomas goto done;
1756 795b88cb 2023-07-10 thomas }
1757 795b88cb 2023-07-10 thomas error = create_ref(remote_refname, id, verbosity - 1, repo);
1758 795b88cb 2023-07-10 thomas free(remote_refname);
1759 795b88cb 2023-07-10 thomas if (error)
1760 795b88cb 2023-07-10 thomas goto done;
1761 795b88cb 2023-07-10 thomas }
1762 795b88cb 2023-07-10 thomas
1763 795b88cb 2023-07-10 thomas /* Set the HEAD reference if the server provided one. */
1764 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &symrefs, entry) {
1765 795b88cb 2023-07-10 thomas struct got_reference *target_ref;
1766 795b88cb 2023-07-10 thomas const char *refname = pe->path;
1767 795b88cb 2023-07-10 thomas const char *target = pe->data;
1768 795b88cb 2023-07-10 thomas char *remote_refname = NULL, *remote_target = NULL;
1769 795b88cb 2023-07-10 thomas
1770 795b88cb 2023-07-10 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
1771 795b88cb 2023-07-10 thomas continue;
1772 795b88cb 2023-07-10 thomas
1773 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1774 795b88cb 2023-07-10 thomas if (error) {
1775 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1776 795b88cb 2023-07-10 thomas error = NULL;
1777 795b88cb 2023-07-10 thomas continue;
1778 795b88cb 2023-07-10 thomas }
1779 795b88cb 2023-07-10 thomas goto done;
1780 795b88cb 2023-07-10 thomas }
1781 795b88cb 2023-07-10 thomas
1782 795b88cb 2023-07-10 thomas error = create_symref(refname, target_ref, verbosity, repo);
1783 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1784 795b88cb 2023-07-10 thomas if (error)
1785 795b88cb 2023-07-10 thomas goto done;
1786 795b88cb 2023-07-10 thomas
1787 795b88cb 2023-07-10 thomas if (mirror_references)
1788 795b88cb 2023-07-10 thomas continue;
1789 795b88cb 2023-07-10 thomas
1790 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", target, 11) != 0)
1791 795b88cb 2023-07-10 thomas continue;
1792 795b88cb 2023-07-10 thomas
1793 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname,
1794 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1795 795b88cb 2023-07-10 thomas refname) == -1) {
1796 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1797 795b88cb 2023-07-10 thomas goto done;
1798 795b88cb 2023-07-10 thomas }
1799 795b88cb 2023-07-10 thomas if (asprintf(&remote_target,
1800 795b88cb 2023-07-10 thomas "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1801 795b88cb 2023-07-10 thomas target + 11) == -1) {
1802 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
1803 795b88cb 2023-07-10 thomas free(remote_refname);
1804 795b88cb 2023-07-10 thomas goto done;
1805 795b88cb 2023-07-10 thomas }
1806 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, remote_target, 0);
1807 795b88cb 2023-07-10 thomas if (error) {
1808 795b88cb 2023-07-10 thomas free(remote_refname);
1809 795b88cb 2023-07-10 thomas free(remote_target);
1810 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1811 795b88cb 2023-07-10 thomas error = NULL;
1812 795b88cb 2023-07-10 thomas continue;
1813 795b88cb 2023-07-10 thomas }
1814 795b88cb 2023-07-10 thomas goto done;
1815 795b88cb 2023-07-10 thomas }
1816 795b88cb 2023-07-10 thomas error = create_symref(remote_refname, target_ref,
1817 795b88cb 2023-07-10 thomas verbosity - 1, repo);
1818 795b88cb 2023-07-10 thomas free(remote_refname);
1819 795b88cb 2023-07-10 thomas free(remote_target);
1820 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1821 795b88cb 2023-07-10 thomas if (error)
1822 795b88cb 2023-07-10 thomas goto done;
1823 795b88cb 2023-07-10 thomas }
1824 795b88cb 2023-07-10 thomas if (pe == NULL) {
1825 795b88cb 2023-07-10 thomas /*
1826 795b88cb 2023-07-10 thomas * We failed to set the HEAD reference. If we asked for
1827 795b88cb 2023-07-10 thomas * a set of wanted branches use the first of one of those
1828 795b88cb 2023-07-10 thomas * which could be fetched instead.
1829 795b88cb 2023-07-10 thomas */
1830 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &wanted_branches, entry) {
1831 795b88cb 2023-07-10 thomas const char *target = pe->path;
1832 795b88cb 2023-07-10 thomas struct got_reference *target_ref;
1833 795b88cb 2023-07-10 thomas
1834 795b88cb 2023-07-10 thomas error = got_ref_open(&target_ref, repo, target, 0);
1835 795b88cb 2023-07-10 thomas if (error) {
1836 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_REF) {
1837 795b88cb 2023-07-10 thomas error = NULL;
1838 795b88cb 2023-07-10 thomas continue;
1839 795b88cb 2023-07-10 thomas }
1840 795b88cb 2023-07-10 thomas goto done;
1841 795b88cb 2023-07-10 thomas }
1842 795b88cb 2023-07-10 thomas
1843 795b88cb 2023-07-10 thomas error = create_symref(GOT_REF_HEAD, target_ref,
1844 795b88cb 2023-07-10 thomas verbosity, repo);
1845 795b88cb 2023-07-10 thomas got_ref_close(target_ref);
1846 795b88cb 2023-07-10 thomas if (error)
1847 795b88cb 2023-07-10 thomas goto done;
1848 795b88cb 2023-07-10 thomas break;
1849 795b88cb 2023-07-10 thomas }
1850 795b88cb 2023-07-10 thomas
1851 795b88cb 2023-07-10 thomas if (!fpa.configs_created && pe != NULL) {
1852 795b88cb 2023-07-10 thomas error = create_config_files(fpa.config_info.proto,
1853 795b88cb 2023-07-10 thomas fpa.config_info.host, fpa.config_info.port,
1854 795b88cb 2023-07-10 thomas fpa.config_info.remote_repo_path,
1855 795b88cb 2023-07-10 thomas fpa.config_info.git_url,
1856 795b88cb 2023-07-10 thomas fpa.config_info.fetch_all_branches,
1857 795b88cb 2023-07-10 thomas fpa.config_info.mirror_references,
1858 795b88cb 2023-07-10 thomas fpa.config_info.symrefs,
1859 795b88cb 2023-07-10 thomas fpa.config_info.wanted_branches,
1860 795b88cb 2023-07-10 thomas fpa.config_info.wanted_refs, fpa.repo);
1861 795b88cb 2023-07-10 thomas if (error)
1862 795b88cb 2023-07-10 thomas goto done;
1863 795b88cb 2023-07-10 thomas }
1864 795b88cb 2023-07-10 thomas }
1865 795b88cb 2023-07-10 thomas
1866 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1867 795b88cb 2023-07-10 thomas printf("Created %s repository '%s'\n",
1868 795b88cb 2023-07-10 thomas mirror_references ? "mirrored" : "cloned", repo_path);
1869 795b88cb 2023-07-10 thomas done:
1870 795b88cb 2023-07-10 thomas if (pack_fds) {
1871 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
1872 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
1873 795b88cb 2023-07-10 thomas if (error == NULL)
1874 795b88cb 2023-07-10 thomas error = pack_err;
1875 795b88cb 2023-07-10 thomas }
1876 795b88cb 2023-07-10 thomas if (fetchpid > 0) {
1877 795b88cb 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
1878 795b88cb 2023-07-10 thomas error = got_error_from_errno("kill");
1879 795b88cb 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1880 795b88cb 2023-07-10 thomas error = got_error_from_errno("waitpid");
1881 795b88cb 2023-07-10 thomas }
1882 795b88cb 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1883 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
1884 795b88cb 2023-07-10 thomas if (repo) {
1885 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
1886 795b88cb 2023-07-10 thomas if (error == NULL)
1887 795b88cb 2023-07-10 thomas error = close_err;
1888 795b88cb 2023-07-10 thomas }
1889 795b88cb 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1890 795b88cb 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1891 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1892 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1893 795b88cb 2023-07-10 thomas free(pack_hash);
1894 795b88cb 2023-07-10 thomas free(proto);
1895 795b88cb 2023-07-10 thomas free(host);
1896 795b88cb 2023-07-10 thomas free(port);
1897 795b88cb 2023-07-10 thomas free(server_path);
1898 795b88cb 2023-07-10 thomas free(repo_name);
1899 795b88cb 2023-07-10 thomas free(default_destdir);
1900 795b88cb 2023-07-10 thomas free(git_url);
1901 795b88cb 2023-07-10 thomas return error;
1902 795b88cb 2023-07-10 thomas }
1903 795b88cb 2023-07-10 thomas
1904 795b88cb 2023-07-10 thomas static const struct got_error *
1905 795b88cb 2023-07-10 thomas update_ref(struct got_reference *ref, struct got_object_id *new_id,
1906 795b88cb 2023-07-10 thomas int replace_tags, int verbosity, struct got_repository *repo)
1907 795b88cb 2023-07-10 thomas {
1908 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
1909 795b88cb 2023-07-10 thomas char *new_id_str = NULL;
1910 795b88cb 2023-07-10 thomas struct got_object_id *old_id = NULL;
1911 795b88cb 2023-07-10 thomas
1912 795b88cb 2023-07-10 thomas err = got_object_id_str(&new_id_str, new_id);
1913 795b88cb 2023-07-10 thomas if (err)
1914 795b88cb 2023-07-10 thomas goto done;
1915 795b88cb 2023-07-10 thomas
1916 795b88cb 2023-07-10 thomas if (!replace_tags &&
1917 795b88cb 2023-07-10 thomas strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1918 795b88cb 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1919 795b88cb 2023-07-10 thomas if (err)
1920 795b88cb 2023-07-10 thomas goto done;
1921 795b88cb 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1922 795b88cb 2023-07-10 thomas goto done;
1923 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
1924 795b88cb 2023-07-10 thomas printf("Rejecting update of existing tag %s: %s\n",
1925 795b88cb 2023-07-10 thomas got_ref_get_name(ref), new_id_str);
1926 795b88cb 2023-07-10 thomas }
1927 795b88cb 2023-07-10 thomas goto done;
1928 795b88cb 2023-07-10 thomas }
1929 795b88cb 2023-07-10 thomas
1930 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(ref)) {
1931 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
1932 795b88cb 2023-07-10 thomas printf("Replacing reference %s: %s\n",
1933 795b88cb 2023-07-10 thomas got_ref_get_name(ref),
1934 795b88cb 2023-07-10 thomas got_ref_get_symref_target(ref));
1935 795b88cb 2023-07-10 thomas }
1936 795b88cb 2023-07-10 thomas err = got_ref_change_symref_to_ref(ref, new_id);
1937 795b88cb 2023-07-10 thomas if (err)
1938 795b88cb 2023-07-10 thomas goto done;
1939 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1940 795b88cb 2023-07-10 thomas if (err)
1941 795b88cb 2023-07-10 thomas goto done;
1942 795b88cb 2023-07-10 thomas } else {
1943 795b88cb 2023-07-10 thomas err = got_ref_resolve(&old_id, repo, ref);
1944 795b88cb 2023-07-10 thomas if (err)
1945 795b88cb 2023-07-10 thomas goto done;
1946 795b88cb 2023-07-10 thomas if (got_object_id_cmp(old_id, new_id) == 0)
1947 795b88cb 2023-07-10 thomas goto done;
1948 795b88cb 2023-07-10 thomas
1949 795b88cb 2023-07-10 thomas err = got_ref_change_ref(ref, new_id);
1950 795b88cb 2023-07-10 thomas if (err)
1951 795b88cb 2023-07-10 thomas goto done;
1952 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
1953 795b88cb 2023-07-10 thomas if (err)
1954 795b88cb 2023-07-10 thomas goto done;
1955 795b88cb 2023-07-10 thomas }
1956 795b88cb 2023-07-10 thomas
1957 795b88cb 2023-07-10 thomas if (verbosity >= 0)
1958 795b88cb 2023-07-10 thomas printf("Updated %s: %s\n", got_ref_get_name(ref),
1959 795b88cb 2023-07-10 thomas new_id_str);
1960 795b88cb 2023-07-10 thomas done:
1961 795b88cb 2023-07-10 thomas free(old_id);
1962 795b88cb 2023-07-10 thomas free(new_id_str);
1963 795b88cb 2023-07-10 thomas return err;
1964 795b88cb 2023-07-10 thomas }
1965 795b88cb 2023-07-10 thomas
1966 795b88cb 2023-07-10 thomas static const struct got_error *
1967 795b88cb 2023-07-10 thomas update_wanted_ref(const char *refname, struct got_object_id *id,
1968 795b88cb 2023-07-10 thomas const char *remote_repo_name, int verbosity, struct got_repository *repo)
1969 795b88cb 2023-07-10 thomas {
1970 795b88cb 2023-07-10 thomas const struct got_error *err, *unlock_err;
1971 795b88cb 2023-07-10 thomas char *remote_refname;
1972 795b88cb 2023-07-10 thomas struct got_reference *ref;
1973 795b88cb 2023-07-10 thomas
1974 795b88cb 2023-07-10 thomas if (strncmp("refs/", refname, 5) == 0)
1975 795b88cb 2023-07-10 thomas refname += 5;
1976 795b88cb 2023-07-10 thomas
1977 795b88cb 2023-07-10 thomas if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1978 795b88cb 2023-07-10 thomas remote_repo_name, refname) == -1)
1979 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
1980 795b88cb 2023-07-10 thomas
1981 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, remote_refname, 1);
1982 795b88cb 2023-07-10 thomas if (err) {
1983 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_NOT_REF)
1984 795b88cb 2023-07-10 thomas goto done;
1985 795b88cb 2023-07-10 thomas err = create_ref(remote_refname, id, verbosity, repo);
1986 795b88cb 2023-07-10 thomas } else {
1987 795b88cb 2023-07-10 thomas err = update_ref(ref, id, 0, verbosity, repo);
1988 795b88cb 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
1989 795b88cb 2023-07-10 thomas if (unlock_err && err == NULL)
1990 795b88cb 2023-07-10 thomas err = unlock_err;
1991 795b88cb 2023-07-10 thomas got_ref_close(ref);
1992 795b88cb 2023-07-10 thomas }
1993 795b88cb 2023-07-10 thomas done:
1994 795b88cb 2023-07-10 thomas free(remote_refname);
1995 795b88cb 2023-07-10 thomas return err;
1996 795b88cb 2023-07-10 thomas }
1997 795b88cb 2023-07-10 thomas
1998 795b88cb 2023-07-10 thomas __dead static void
1999 795b88cb 2023-07-10 thomas usage_checkout(void)
2000 795b88cb 2023-07-10 thomas {
2001 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2002 795b88cb 2023-07-10 thomas "[-p path-prefix] repository-path [work-tree-path]\n",
2003 795b88cb 2023-07-10 thomas getprogname());
2004 795b88cb 2023-07-10 thomas exit(1);
2005 795b88cb 2023-07-10 thomas }
2006 795b88cb 2023-07-10 thomas
2007 795b88cb 2023-07-10 thomas static void
2008 795b88cb 2023-07-10 thomas show_worktree_base_ref_warning(void)
2009 795b88cb 2023-07-10 thomas {
2010 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: warning: could not create a reference "
2011 795b88cb 2023-07-10 thomas "to the work tree's base commit; the commit could be "
2012 795b88cb 2023-07-10 thomas "garbage-collected by Git or 'gotadmin cleanup'; making the "
2013 795b88cb 2023-07-10 thomas "repository writable and running 'got update' will prevent this\n",
2014 795b88cb 2023-07-10 thomas getprogname());
2015 795b88cb 2023-07-10 thomas }
2016 795b88cb 2023-07-10 thomas
2017 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg {
2018 795b88cb 2023-07-10 thomas const char *worktree_path;
2019 795b88cb 2023-07-10 thomas int had_base_commit_ref_error;
2020 795b88cb 2023-07-10 thomas int verbosity;
2021 795b88cb 2023-07-10 thomas };
2022 795b88cb 2023-07-10 thomas
2023 795b88cb 2023-07-10 thomas static const struct got_error *
2024 795b88cb 2023-07-10 thomas checkout_progress(void *arg, unsigned char status, const char *path)
2025 795b88cb 2023-07-10 thomas {
2026 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg *a = arg;
2027 795b88cb 2023-07-10 thomas
2028 795b88cb 2023-07-10 thomas /* Base commit bump happens silently. */
2029 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2030 795b88cb 2023-07-10 thomas return NULL;
2031 795b88cb 2023-07-10 thomas
2032 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BASE_REF_ERR) {
2033 795b88cb 2023-07-10 thomas a->had_base_commit_ref_error = 1;
2034 795b88cb 2023-07-10 thomas return NULL;
2035 795b88cb 2023-07-10 thomas }
2036 795b88cb 2023-07-10 thomas
2037 795b88cb 2023-07-10 thomas while (path[0] == '/')
2038 795b88cb 2023-07-10 thomas path++;
2039 795b88cb 2023-07-10 thomas
2040 795b88cb 2023-07-10 thomas if (a->verbosity >= 0)
2041 795b88cb 2023-07-10 thomas printf("%c %s/%s\n", status, a->worktree_path, path);
2042 795b88cb 2023-07-10 thomas
2043 795b88cb 2023-07-10 thomas return NULL;
2044 795b88cb 2023-07-10 thomas }
2045 795b88cb 2023-07-10 thomas
2046 795b88cb 2023-07-10 thomas static const struct got_error *
2047 795b88cb 2023-07-10 thomas check_cancelled(void *arg)
2048 795b88cb 2023-07-10 thomas {
2049 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
2050 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_CANCELLED);
2051 795b88cb 2023-07-10 thomas return NULL;
2052 795b88cb 2023-07-10 thomas }
2053 795b88cb 2023-07-10 thomas
2054 795b88cb 2023-07-10 thomas static const struct got_error *
2055 795b88cb 2023-07-10 thomas check_linear_ancestry(struct got_object_id *commit_id,
2056 795b88cb 2023-07-10 thomas struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2057 795b88cb 2023-07-10 thomas struct got_repository *repo)
2058 795b88cb 2023-07-10 thomas {
2059 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2060 795b88cb 2023-07-10 thomas struct got_object_id *yca_id;
2061 795b88cb 2023-07-10 thomas
2062 795b88cb 2023-07-10 thomas err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2063 795b88cb 2023-07-10 thomas commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2064 795b88cb 2023-07-10 thomas if (err)
2065 795b88cb 2023-07-10 thomas return err;
2066 795b88cb 2023-07-10 thomas
2067 795b88cb 2023-07-10 thomas if (yca_id == NULL)
2068 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2069 795b88cb 2023-07-10 thomas
2070 795b88cb 2023-07-10 thomas /*
2071 795b88cb 2023-07-10 thomas * Require a straight line of history between the target commit
2072 795b88cb 2023-07-10 thomas * and the work tree's base commit.
2073 795b88cb 2023-07-10 thomas *
2074 795b88cb 2023-07-10 thomas * Non-linear situations such as this require a rebase:
2075 795b88cb 2023-07-10 thomas *
2076 795b88cb 2023-07-10 thomas * (commit) D F (base_commit)
2077 795b88cb 2023-07-10 thomas * \ /
2078 795b88cb 2023-07-10 thomas * C E
2079 795b88cb 2023-07-10 thomas * \ /
2080 795b88cb 2023-07-10 thomas * B (yca)
2081 795b88cb 2023-07-10 thomas * |
2082 795b88cb 2023-07-10 thomas * A
2083 795b88cb 2023-07-10 thomas *
2084 795b88cb 2023-07-10 thomas * 'got update' only handles linear cases:
2085 795b88cb 2023-07-10 thomas * Update forwards in time: A (base/yca) - B - C - D (commit)
2086 795b88cb 2023-07-10 thomas * Update backwards in time: D (base) - C - B - A (commit/yca)
2087 795b88cb 2023-07-10 thomas */
2088 795b88cb 2023-07-10 thomas if (allow_forwards_in_time_only) {
2089 795b88cb 2023-07-10 thomas if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2090 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2091 795b88cb 2023-07-10 thomas } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2092 795b88cb 2023-07-10 thomas got_object_id_cmp(base_commit_id, yca_id) != 0)
2093 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ANCESTRY);
2094 795b88cb 2023-07-10 thomas
2095 795b88cb 2023-07-10 thomas free(yca_id);
2096 795b88cb 2023-07-10 thomas return NULL;
2097 795b88cb 2023-07-10 thomas }
2098 795b88cb 2023-07-10 thomas
2099 795b88cb 2023-07-10 thomas static const struct got_error *
2100 795b88cb 2023-07-10 thomas check_same_branch(struct got_object_id *commit_id,
2101 795b88cb 2023-07-10 thomas struct got_reference *head_ref, struct got_repository *repo)
2102 795b88cb 2023-07-10 thomas {
2103 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2104 795b88cb 2023-07-10 thomas struct got_commit_graph *graph = NULL;
2105 795b88cb 2023-07-10 thomas struct got_object_id *head_commit_id = NULL;
2106 795b88cb 2023-07-10 thomas
2107 795b88cb 2023-07-10 thomas err = got_ref_resolve(&head_commit_id, repo, head_ref);
2108 795b88cb 2023-07-10 thomas if (err)
2109 795b88cb 2023-07-10 thomas goto done;
2110 795b88cb 2023-07-10 thomas
2111 795b88cb 2023-07-10 thomas if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2112 795b88cb 2023-07-10 thomas goto done;
2113 795b88cb 2023-07-10 thomas
2114 795b88cb 2023-07-10 thomas err = got_commit_graph_open(&graph, "/", 1);
2115 795b88cb 2023-07-10 thomas if (err)
2116 795b88cb 2023-07-10 thomas goto done;
2117 795b88cb 2023-07-10 thomas
2118 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2119 795b88cb 2023-07-10 thomas check_cancelled, NULL);
2120 795b88cb 2023-07-10 thomas if (err)
2121 795b88cb 2023-07-10 thomas goto done;
2122 795b88cb 2023-07-10 thomas
2123 795b88cb 2023-07-10 thomas for (;;) {
2124 795b88cb 2023-07-10 thomas struct got_object_id id;
2125 795b88cb 2023-07-10 thomas
2126 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
2127 795b88cb 2023-07-10 thomas check_cancelled, NULL);
2128 795b88cb 2023-07-10 thomas if (err) {
2129 795b88cb 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
2130 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_ANCESTRY);
2131 795b88cb 2023-07-10 thomas break;
2132 795b88cb 2023-07-10 thomas }
2133 795b88cb 2023-07-10 thomas
2134 795b88cb 2023-07-10 thomas if (got_object_id_cmp(&id, commit_id) == 0)
2135 795b88cb 2023-07-10 thomas break;
2136 795b88cb 2023-07-10 thomas }
2137 795b88cb 2023-07-10 thomas done:
2138 795b88cb 2023-07-10 thomas if (graph)
2139 795b88cb 2023-07-10 thomas got_commit_graph_close(graph);
2140 795b88cb 2023-07-10 thomas free(head_commit_id);
2141 795b88cb 2023-07-10 thomas return err;
2142 795b88cb 2023-07-10 thomas }
2143 795b88cb 2023-07-10 thomas
2144 795b88cb 2023-07-10 thomas static const struct got_error *
2145 795b88cb 2023-07-10 thomas checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2146 795b88cb 2023-07-10 thomas {
2147 795b88cb 2023-07-10 thomas static char msg[512];
2148 795b88cb 2023-07-10 thomas const char *branch_name;
2149 795b88cb 2023-07-10 thomas
2150 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(ref))
2151 795b88cb 2023-07-10 thomas branch_name = got_ref_get_symref_target(ref);
2152 795b88cb 2023-07-10 thomas else
2153 795b88cb 2023-07-10 thomas branch_name = got_ref_get_name(ref);
2154 795b88cb 2023-07-10 thomas
2155 795b88cb 2023-07-10 thomas if (strncmp("refs/heads/", branch_name, 11) == 0)
2156 795b88cb 2023-07-10 thomas branch_name += 11;
2157 795b88cb 2023-07-10 thomas
2158 795b88cb 2023-07-10 thomas snprintf(msg, sizeof(msg),
2159 795b88cb 2023-07-10 thomas "target commit is not contained in branch '%s'; "
2160 795b88cb 2023-07-10 thomas "the branch to use must be specified with -b; "
2161 795b88cb 2023-07-10 thomas "if necessary a new branch can be created for "
2162 795b88cb 2023-07-10 thomas "this commit with 'got branch -c %s BRANCH_NAME'",
2163 795b88cb 2023-07-10 thomas branch_name, commit_id_str);
2164 795b88cb 2023-07-10 thomas
2165 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_ANCESTRY, msg);
2166 795b88cb 2023-07-10 thomas }
2167 795b88cb 2023-07-10 thomas
2168 795b88cb 2023-07-10 thomas static const struct got_error *
2169 795b88cb 2023-07-10 thomas cmd_checkout(int argc, char *argv[])
2170 795b88cb 2023-07-10 thomas {
2171 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
2172 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
2173 795b88cb 2023-07-10 thomas struct got_reference *head_ref = NULL, *ref = NULL;
2174 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
2175 795b88cb 2023-07-10 thomas char *repo_path = NULL;
2176 795b88cb 2023-07-10 thomas char *worktree_path = NULL;
2177 795b88cb 2023-07-10 thomas const char *path_prefix = "";
2178 795b88cb 2023-07-10 thomas const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2179 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
2180 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2181 795b88cb 2023-07-10 thomas char *cwd = NULL;
2182 795b88cb 2023-07-10 thomas int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2183 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
2184 795b88cb 2023-07-10 thomas struct got_checkout_progress_arg cpa;
2185 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2186 795b88cb 2023-07-10 thomas
2187 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
2188 795b88cb 2023-07-10 thomas
2189 795b88cb 2023-07-10 thomas #ifndef PROFILE
2190 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2191 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
2192 795b88cb 2023-07-10 thomas err(1, "pledge");
2193 795b88cb 2023-07-10 thomas #endif
2194 795b88cb 2023-07-10 thomas
2195 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2196 795b88cb 2023-07-10 thomas switch (ch) {
2197 795b88cb 2023-07-10 thomas case 'b':
2198 795b88cb 2023-07-10 thomas branch_name = optarg;
2199 795b88cb 2023-07-10 thomas break;
2200 795b88cb 2023-07-10 thomas case 'c':
2201 795b88cb 2023-07-10 thomas commit_id_str = strdup(optarg);
2202 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
2203 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2204 795b88cb 2023-07-10 thomas break;
2205 795b88cb 2023-07-10 thomas case 'E':
2206 795b88cb 2023-07-10 thomas allow_nonempty = 1;
2207 795b88cb 2023-07-10 thomas break;
2208 795b88cb 2023-07-10 thomas case 'p':
2209 795b88cb 2023-07-10 thomas path_prefix = optarg;
2210 795b88cb 2023-07-10 thomas break;
2211 795b88cb 2023-07-10 thomas case 'q':
2212 795b88cb 2023-07-10 thomas verbosity = -1;
2213 795b88cb 2023-07-10 thomas break;
2214 795b88cb 2023-07-10 thomas default:
2215 795b88cb 2023-07-10 thomas usage_checkout();
2216 795b88cb 2023-07-10 thomas /* NOTREACHED */
2217 795b88cb 2023-07-10 thomas }
2218 795b88cb 2023-07-10 thomas }
2219 795b88cb 2023-07-10 thomas
2220 795b88cb 2023-07-10 thomas argc -= optind;
2221 795b88cb 2023-07-10 thomas argv += optind;
2222 795b88cb 2023-07-10 thomas
2223 795b88cb 2023-07-10 thomas if (argc == 1) {
2224 795b88cb 2023-07-10 thomas char *base, *dotgit;
2225 795b88cb 2023-07-10 thomas const char *path;
2226 795b88cb 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
2227 795b88cb 2023-07-10 thomas if (repo_path == NULL)
2228 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath", argv[0]);
2229 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
2230 795b88cb 2023-07-10 thomas if (cwd == NULL) {
2231 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
2232 795b88cb 2023-07-10 thomas goto done;
2233 795b88cb 2023-07-10 thomas }
2234 795b88cb 2023-07-10 thomas if (path_prefix[0])
2235 795b88cb 2023-07-10 thomas path = path_prefix;
2236 795b88cb 2023-07-10 thomas else
2237 795b88cb 2023-07-10 thomas path = repo_path;
2238 795b88cb 2023-07-10 thomas error = got_path_basename(&base, path);
2239 795b88cb 2023-07-10 thomas if (error)
2240 795b88cb 2023-07-10 thomas goto done;
2241 795b88cb 2023-07-10 thomas dotgit = strstr(base, ".git");
2242 795b88cb 2023-07-10 thomas if (dotgit)
2243 795b88cb 2023-07-10 thomas *dotgit = '\0';
2244 795b88cb 2023-07-10 thomas if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2245 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
2246 795b88cb 2023-07-10 thomas free(base);
2247 795b88cb 2023-07-10 thomas goto done;
2248 795b88cb 2023-07-10 thomas }
2249 795b88cb 2023-07-10 thomas free(base);
2250 795b88cb 2023-07-10 thomas } else if (argc == 2) {
2251 795b88cb 2023-07-10 thomas repo_path = realpath(argv[0], NULL);
2252 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
2253 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath", argv[0]);
2254 795b88cb 2023-07-10 thomas goto done;
2255 795b88cb 2023-07-10 thomas }
2256 795b88cb 2023-07-10 thomas worktree_path = realpath(argv[1], NULL);
2257 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2258 795b88cb 2023-07-10 thomas if (errno != ENOENT) {
2259 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
2260 795b88cb 2023-07-10 thomas argv[1]);
2261 795b88cb 2023-07-10 thomas goto done;
2262 795b88cb 2023-07-10 thomas }
2263 795b88cb 2023-07-10 thomas worktree_path = strdup(argv[1]);
2264 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2265 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
2266 795b88cb 2023-07-10 thomas goto done;
2267 795b88cb 2023-07-10 thomas }
2268 795b88cb 2023-07-10 thomas }
2269 795b88cb 2023-07-10 thomas } else
2270 795b88cb 2023-07-10 thomas usage_checkout();
2271 795b88cb 2023-07-10 thomas
2272 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
2273 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(worktree_path);
2274 795b88cb 2023-07-10 thomas
2275 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2276 795b88cb 2023-07-10 thomas if (error != NULL)
2277 795b88cb 2023-07-10 thomas goto done;
2278 795b88cb 2023-07-10 thomas
2279 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, 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 /* Pre-create work tree path for unveil(2) */
2284 795b88cb 2023-07-10 thomas error = got_path_mkdir(worktree_path);
2285 795b88cb 2023-07-10 thomas if (error) {
2286 795b88cb 2023-07-10 thomas if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2287 795b88cb 2023-07-10 thomas !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2288 795b88cb 2023-07-10 thomas goto done;
2289 795b88cb 2023-07-10 thomas if (!allow_nonempty &&
2290 795b88cb 2023-07-10 thomas !got_path_dir_is_empty(worktree_path)) {
2291 795b88cb 2023-07-10 thomas error = got_error_path(worktree_path,
2292 795b88cb 2023-07-10 thomas GOT_ERR_DIR_NOT_EMPTY);
2293 795b88cb 2023-07-10 thomas goto done;
2294 795b88cb 2023-07-10 thomas }
2295 795b88cb 2023-07-10 thomas }
2296 795b88cb 2023-07-10 thomas
2297 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2298 795b88cb 2023-07-10 thomas if (error)
2299 795b88cb 2023-07-10 thomas goto done;
2300 795b88cb 2023-07-10 thomas
2301 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, branch_name, 0);
2302 795b88cb 2023-07-10 thomas if (error != NULL)
2303 795b88cb 2023-07-10 thomas goto done;
2304 795b88cb 2023-07-10 thomas
2305 ad10f64e 2023-07-19 thomas error = got_worktree_init(worktree_path, head_ref, path_prefix,
2306 ad10f64e 2023-07-19 thomas GOT_WORKTREE_CVG_DIR, repo);
2307 795b88cb 2023-07-10 thomas if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2308 795b88cb 2023-07-10 thomas goto done;
2309 795b88cb 2023-07-10 thomas
2310 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, worktree_path, GOT_WORKTREE_CVG_DIR);
2311 795b88cb 2023-07-10 thomas if (error != NULL)
2312 795b88cb 2023-07-10 thomas goto done;
2313 795b88cb 2023-07-10 thomas
2314 795b88cb 2023-07-10 thomas error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2315 795b88cb 2023-07-10 thomas path_prefix);
2316 795b88cb 2023-07-10 thomas if (error != NULL)
2317 795b88cb 2023-07-10 thomas goto done;
2318 795b88cb 2023-07-10 thomas if (!same_path_prefix) {
2319 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_PATH_PREFIX);
2320 795b88cb 2023-07-10 thomas goto done;
2321 795b88cb 2023-07-10 thomas }
2322 795b88cb 2023-07-10 thomas
2323 795b88cb 2023-07-10 thomas if (commit_id_str) {
2324 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
2325 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2326 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2327 795b88cb 2023-07-10 thomas NULL);
2328 795b88cb 2023-07-10 thomas if (error)
2329 795b88cb 2023-07-10 thomas goto done;
2330 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
2331 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2332 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
2333 795b88cb 2023-07-10 thomas if (error)
2334 795b88cb 2023-07-10 thomas goto done;
2335 795b88cb 2023-07-10 thomas error = check_linear_ancestry(commit_id,
2336 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
2337 795b88cb 2023-07-10 thomas if (error != NULL) {
2338 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
2339 795b88cb 2023-07-10 thomas error = checkout_ancestry_error(
2340 795b88cb 2023-07-10 thomas head_ref, commit_id_str);
2341 795b88cb 2023-07-10 thomas }
2342 795b88cb 2023-07-10 thomas goto done;
2343 795b88cb 2023-07-10 thomas }
2344 795b88cb 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
2345 795b88cb 2023-07-10 thomas if (error) {
2346 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY) {
2347 795b88cb 2023-07-10 thomas error = checkout_ancestry_error(
2348 795b88cb 2023-07-10 thomas head_ref, commit_id_str);
2349 795b88cb 2023-07-10 thomas }
2350 795b88cb 2023-07-10 thomas goto done;
2351 795b88cb 2023-07-10 thomas }
2352 795b88cb 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
2353 795b88cb 2023-07-10 thomas commit_id);
2354 795b88cb 2023-07-10 thomas if (error)
2355 795b88cb 2023-07-10 thomas goto done;
2356 795b88cb 2023-07-10 thomas /* Expand potentially abbreviated commit ID string. */
2357 795b88cb 2023-07-10 thomas free(commit_id_str);
2358 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2359 795b88cb 2023-07-10 thomas if (error)
2360 795b88cb 2023-07-10 thomas goto done;
2361 795b88cb 2023-07-10 thomas } else {
2362 795b88cb 2023-07-10 thomas commit_id = got_object_id_dup(
2363 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
2364 795b88cb 2023-07-10 thomas if (commit_id == NULL) {
2365 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_object_id_dup");
2366 795b88cb 2023-07-10 thomas goto done;
2367 795b88cb 2023-07-10 thomas }
2368 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2369 795b88cb 2023-07-10 thomas if (error)
2370 795b88cb 2023-07-10 thomas goto done;
2371 795b88cb 2023-07-10 thomas }
2372 795b88cb 2023-07-10 thomas
2373 795b88cb 2023-07-10 thomas error = got_pathlist_append(&paths, "", NULL);
2374 795b88cb 2023-07-10 thomas if (error)
2375 795b88cb 2023-07-10 thomas goto done;
2376 795b88cb 2023-07-10 thomas cpa.worktree_path = worktree_path;
2377 795b88cb 2023-07-10 thomas cpa.had_base_commit_ref_error = 0;
2378 795b88cb 2023-07-10 thomas cpa.verbosity = verbosity;
2379 795b88cb 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
2380 795b88cb 2023-07-10 thomas checkout_progress, &cpa, check_cancelled, NULL);
2381 795b88cb 2023-07-10 thomas if (error != NULL)
2382 795b88cb 2023-07-10 thomas goto done;
2383 795b88cb 2023-07-10 thomas
2384 795b88cb 2023-07-10 thomas if (got_ref_is_symbolic(head_ref)) {
2385 795b88cb 2023-07-10 thomas error = got_ref_resolve_symbolic(&ref, repo, head_ref);
2386 795b88cb 2023-07-10 thomas if (error)
2387 795b88cb 2023-07-10 thomas goto done;
2388 795b88cb 2023-07-10 thomas refname = got_ref_get_name(ref);
2389 795b88cb 2023-07-10 thomas } else
2390 795b88cb 2023-07-10 thomas refname = got_ref_get_name(head_ref);
2391 795b88cb 2023-07-10 thomas printf("Checked out %s: %s\n", refname, commit_id_str);
2392 795b88cb 2023-07-10 thomas printf("Now shut up and hack\n");
2393 795b88cb 2023-07-10 thomas if (cpa.had_base_commit_ref_error)
2394 795b88cb 2023-07-10 thomas show_worktree_base_ref_warning();
2395 795b88cb 2023-07-10 thomas done:
2396 795b88cb 2023-07-10 thomas if (pack_fds) {
2397 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
2398 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2399 795b88cb 2023-07-10 thomas if (error == NULL)
2400 795b88cb 2023-07-10 thomas error = pack_err;
2401 795b88cb 2023-07-10 thomas }
2402 795b88cb 2023-07-10 thomas if (head_ref)
2403 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
2404 795b88cb 2023-07-10 thomas if (ref)
2405 795b88cb 2023-07-10 thomas got_ref_close(ref);
2406 795b88cb 2023-07-10 thomas if (repo) {
2407 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2408 795b88cb 2023-07-10 thomas if (error == NULL)
2409 795b88cb 2023-07-10 thomas error = close_err;
2410 795b88cb 2023-07-10 thomas }
2411 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
2412 795b88cb 2023-07-10 thomas free(commit_id_str);
2413 795b88cb 2023-07-10 thomas free(commit_id);
2414 795b88cb 2023-07-10 thomas free(repo_path);
2415 795b88cb 2023-07-10 thomas free(worktree_path);
2416 795b88cb 2023-07-10 thomas free(cwd);
2417 795b88cb 2023-07-10 thomas return error;
2418 795b88cb 2023-07-10 thomas }
2419 795b88cb 2023-07-10 thomas
2420 795b88cb 2023-07-10 thomas struct got_update_progress_arg {
2421 795b88cb 2023-07-10 thomas int did_something;
2422 795b88cb 2023-07-10 thomas int conflicts;
2423 795b88cb 2023-07-10 thomas int obstructed;
2424 795b88cb 2023-07-10 thomas int not_updated;
2425 795b88cb 2023-07-10 thomas int missing;
2426 795b88cb 2023-07-10 thomas int not_deleted;
2427 795b88cb 2023-07-10 thomas int unversioned;
2428 795b88cb 2023-07-10 thomas int verbosity;
2429 795b88cb 2023-07-10 thomas };
2430 795b88cb 2023-07-10 thomas
2431 795b88cb 2023-07-10 thomas static void
2432 795b88cb 2023-07-10 thomas print_update_progress_stats(struct got_update_progress_arg *upa)
2433 795b88cb 2023-07-10 thomas {
2434 795b88cb 2023-07-10 thomas if (!upa->did_something)
2435 795b88cb 2023-07-10 thomas return;
2436 795b88cb 2023-07-10 thomas
2437 795b88cb 2023-07-10 thomas if (upa->conflicts > 0)
2438 795b88cb 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
2439 795b88cb 2023-07-10 thomas if (upa->obstructed > 0)
2440 795b88cb 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
2441 795b88cb 2023-07-10 thomas upa->obstructed);
2442 795b88cb 2023-07-10 thomas if (upa->not_updated > 0)
2443 795b88cb 2023-07-10 thomas printf("Files not updated because of existing merge "
2444 795b88cb 2023-07-10 thomas "conflicts: %d\n", upa->not_updated);
2445 795b88cb 2023-07-10 thomas }
2446 795b88cb 2023-07-10 thomas
2447 795b88cb 2023-07-10 thomas /*
2448 795b88cb 2023-07-10 thomas * The meaning of some status codes differs between merge-style operations and
2449 795b88cb 2023-07-10 thomas * update operations. For example, the ! status code means "file was missing"
2450 795b88cb 2023-07-10 thomas * if changes were merged into the work tree, and "missing file was restored"
2451 795b88cb 2023-07-10 thomas * if the work tree was updated. This function should be used by any operation
2452 795b88cb 2023-07-10 thomas * which merges changes into the work tree without updating the work tree.
2453 795b88cb 2023-07-10 thomas */
2454 795b88cb 2023-07-10 thomas static void
2455 795b88cb 2023-07-10 thomas print_merge_progress_stats(struct got_update_progress_arg *upa)
2456 795b88cb 2023-07-10 thomas {
2457 795b88cb 2023-07-10 thomas if (!upa->did_something)
2458 795b88cb 2023-07-10 thomas return;
2459 795b88cb 2023-07-10 thomas
2460 795b88cb 2023-07-10 thomas if (upa->conflicts > 0)
2461 795b88cb 2023-07-10 thomas printf("Files with new merge conflicts: %d\n", upa->conflicts);
2462 795b88cb 2023-07-10 thomas if (upa->obstructed > 0)
2463 795b88cb 2023-07-10 thomas printf("File paths obstructed by a non-regular file: %d\n",
2464 795b88cb 2023-07-10 thomas upa->obstructed);
2465 795b88cb 2023-07-10 thomas if (upa->missing > 0)
2466 795b88cb 2023-07-10 thomas printf("Files which had incoming changes but could not be "
2467 795b88cb 2023-07-10 thomas "found in the work tree: %d\n", upa->missing);
2468 795b88cb 2023-07-10 thomas if (upa->not_deleted > 0)
2469 795b88cb 2023-07-10 thomas printf("Files not deleted due to differences in deleted "
2470 795b88cb 2023-07-10 thomas "content: %d\n", upa->not_deleted);
2471 795b88cb 2023-07-10 thomas if (upa->unversioned > 0)
2472 795b88cb 2023-07-10 thomas printf("Files not merged because an unversioned file was "
2473 795b88cb 2023-07-10 thomas "found in the work tree: %d\n", upa->unversioned);
2474 795b88cb 2023-07-10 thomas }
2475 795b88cb 2023-07-10 thomas
2476 795b88cb 2023-07-10 thomas __dead static void
2477 795b88cb 2023-07-10 thomas usage_update(void)
2478 795b88cb 2023-07-10 thomas {
2479 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s update [-qtvX] [-c commit] [-r remote] "
2480 795b88cb 2023-07-10 thomas "[path ...]\n", getprogname());
2481 795b88cb 2023-07-10 thomas exit(1);
2482 795b88cb 2023-07-10 thomas }
2483 795b88cb 2023-07-10 thomas
2484 795b88cb 2023-07-10 thomas static const struct got_error *
2485 795b88cb 2023-07-10 thomas update_progress(void *arg, unsigned char status, const char *path)
2486 795b88cb 2023-07-10 thomas {
2487 795b88cb 2023-07-10 thomas struct got_update_progress_arg *upa = arg;
2488 795b88cb 2023-07-10 thomas
2489 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_EXISTS ||
2490 795b88cb 2023-07-10 thomas status == GOT_STATUS_BASE_REF_ERR)
2491 795b88cb 2023-07-10 thomas return NULL;
2492 795b88cb 2023-07-10 thomas
2493 795b88cb 2023-07-10 thomas upa->did_something = 1;
2494 795b88cb 2023-07-10 thomas
2495 795b88cb 2023-07-10 thomas /* Base commit bump happens silently. */
2496 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_BUMP_BASE)
2497 795b88cb 2023-07-10 thomas return NULL;
2498 795b88cb 2023-07-10 thomas
2499 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CONFLICT)
2500 795b88cb 2023-07-10 thomas upa->conflicts++;
2501 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_OBSTRUCTED)
2502 795b88cb 2023-07-10 thomas upa->obstructed++;
2503 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
2504 795b88cb 2023-07-10 thomas upa->not_updated++;
2505 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_MISSING)
2506 795b88cb 2023-07-10 thomas upa->missing++;
2507 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_DELETE)
2508 795b88cb 2023-07-10 thomas upa->not_deleted++;
2509 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
2510 795b88cb 2023-07-10 thomas upa->unversioned++;
2511 795b88cb 2023-07-10 thomas
2512 795b88cb 2023-07-10 thomas while (path[0] == '/')
2513 795b88cb 2023-07-10 thomas path++;
2514 795b88cb 2023-07-10 thomas if (upa->verbosity >= 0)
2515 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
2516 795b88cb 2023-07-10 thomas
2517 795b88cb 2023-07-10 thomas return NULL;
2518 795b88cb 2023-07-10 thomas }
2519 795b88cb 2023-07-10 thomas
2520 795b88cb 2023-07-10 thomas static const struct got_error *
2521 795b88cb 2023-07-10 thomas check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2522 795b88cb 2023-07-10 thomas {
2523 795b88cb 2023-07-10 thomas const struct got_error *err;
2524 795b88cb 2023-07-10 thomas int in_progress;
2525 795b88cb 2023-07-10 thomas
2526 795b88cb 2023-07-10 thomas err = got_worktree_rebase_in_progress(&in_progress, worktree);
2527 795b88cb 2023-07-10 thomas if (err)
2528 795b88cb 2023-07-10 thomas return err;
2529 795b88cb 2023-07-10 thomas if (in_progress)
2530 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_REBASING);
2531 795b88cb 2023-07-10 thomas
2532 795b88cb 2023-07-10 thomas err = got_worktree_histedit_in_progress(&in_progress, worktree);
2533 795b88cb 2023-07-10 thomas if (err)
2534 795b88cb 2023-07-10 thomas return err;
2535 795b88cb 2023-07-10 thomas if (in_progress)
2536 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_HISTEDIT_BUSY);
2537 795b88cb 2023-07-10 thomas
2538 795b88cb 2023-07-10 thomas return NULL;
2539 795b88cb 2023-07-10 thomas }
2540 795b88cb 2023-07-10 thomas
2541 795b88cb 2023-07-10 thomas static const struct got_error *
2542 795b88cb 2023-07-10 thomas check_merge_in_progress(struct got_worktree *worktree,
2543 795b88cb 2023-07-10 thomas struct got_repository *repo)
2544 795b88cb 2023-07-10 thomas {
2545 795b88cb 2023-07-10 thomas const struct got_error *err;
2546 795b88cb 2023-07-10 thomas int in_progress;
2547 795b88cb 2023-07-10 thomas
2548 795b88cb 2023-07-10 thomas err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
2549 795b88cb 2023-07-10 thomas if (err)
2550 795b88cb 2023-07-10 thomas return err;
2551 795b88cb 2023-07-10 thomas if (in_progress)
2552 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_MERGE_BUSY);
2553 795b88cb 2023-07-10 thomas
2554 795b88cb 2023-07-10 thomas return NULL;
2555 795b88cb 2023-07-10 thomas }
2556 795b88cb 2023-07-10 thomas
2557 795b88cb 2023-07-10 thomas static const struct got_error *
2558 795b88cb 2023-07-10 thomas get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2559 795b88cb 2023-07-10 thomas char *argv[], struct got_worktree *worktree)
2560 795b88cb 2023-07-10 thomas {
2561 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
2562 795b88cb 2023-07-10 thomas char *path;
2563 795b88cb 2023-07-10 thomas struct got_pathlist_entry *new;
2564 795b88cb 2023-07-10 thomas int i;
2565 795b88cb 2023-07-10 thomas
2566 795b88cb 2023-07-10 thomas if (argc == 0) {
2567 795b88cb 2023-07-10 thomas path = strdup("");
2568 795b88cb 2023-07-10 thomas if (path == NULL)
2569 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2570 795b88cb 2023-07-10 thomas return got_pathlist_append(paths, path, NULL);
2571 795b88cb 2023-07-10 thomas }
2572 795b88cb 2023-07-10 thomas
2573 795b88cb 2023-07-10 thomas for (i = 0; i < argc; i++) {
2574 795b88cb 2023-07-10 thomas err = got_worktree_resolve_path(&path, worktree, argv[i]);
2575 795b88cb 2023-07-10 thomas if (err)
2576 795b88cb 2023-07-10 thomas break;
2577 795b88cb 2023-07-10 thomas err = got_pathlist_insert(&new, paths, path, NULL);
2578 795b88cb 2023-07-10 thomas if (err || new == NULL /* duplicate */) {
2579 795b88cb 2023-07-10 thomas free(path);
2580 795b88cb 2023-07-10 thomas if (err)
2581 795b88cb 2023-07-10 thomas break;
2582 795b88cb 2023-07-10 thomas }
2583 795b88cb 2023-07-10 thomas }
2584 795b88cb 2023-07-10 thomas
2585 795b88cb 2023-07-10 thomas return err;
2586 795b88cb 2023-07-10 thomas }
2587 795b88cb 2023-07-10 thomas
2588 795b88cb 2023-07-10 thomas static const struct got_error *
2589 795b88cb 2023-07-10 thomas wrap_not_worktree_error(const struct got_error *orig_err,
2590 795b88cb 2023-07-10 thomas const char *cmdname, const char *path)
2591 795b88cb 2023-07-10 thomas {
2592 795b88cb 2023-07-10 thomas const struct got_error *err;
2593 795b88cb 2023-07-10 thomas struct got_repository *repo;
2594 795b88cb 2023-07-10 thomas static char msg[512];
2595 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2596 795b88cb 2023-07-10 thomas
2597 795b88cb 2023-07-10 thomas err = got_repo_pack_fds_open(&pack_fds);
2598 795b88cb 2023-07-10 thomas if (err)
2599 795b88cb 2023-07-10 thomas return err;
2600 795b88cb 2023-07-10 thomas
2601 795b88cb 2023-07-10 thomas err = got_repo_open(&repo, path, NULL, pack_fds);
2602 795b88cb 2023-07-10 thomas if (err)
2603 795b88cb 2023-07-10 thomas return orig_err;
2604 795b88cb 2023-07-10 thomas
2605 795b88cb 2023-07-10 thomas snprintf(msg, sizeof(msg),
2606 795b88cb 2023-07-10 thomas "'got %s' needs a work tree in addition to a git repository\n"
2607 795b88cb 2023-07-10 thomas "Work trees can be checked out from this Git repository with "
2608 795b88cb 2023-07-10 thomas "'got checkout'.\n"
2609 795b88cb 2023-07-10 thomas "The got(1) manual page contains more information.", cmdname);
2610 795b88cb 2023-07-10 thomas err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2611 795b88cb 2023-07-10 thomas if (repo) {
2612 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
2613 795b88cb 2023-07-10 thomas if (err == NULL)
2614 795b88cb 2023-07-10 thomas err = close_err;
2615 795b88cb 2023-07-10 thomas }
2616 795b88cb 2023-07-10 thomas if (pack_fds) {
2617 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
2618 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
2619 795b88cb 2023-07-10 thomas if (err == NULL)
2620 795b88cb 2023-07-10 thomas err = pack_err;
2621 795b88cb 2023-07-10 thomas }
2622 795b88cb 2023-07-10 thomas return err;
2623 795b88cb 2023-07-10 thomas }
2624 795b88cb 2023-07-10 thomas
2625 795b88cb 2023-07-10 thomas static const struct got_error *
2626 795b88cb 2023-07-10 thomas cmd_update(int argc, char *argv[])
2627 795b88cb 2023-07-10 thomas {
2628 795b88cb 2023-07-10 thomas const struct got_error *error = NULL, *unlock_err;
2629 795b88cb 2023-07-10 thomas char *worktree_path = NULL;
2630 795b88cb 2023-07-10 thomas const char *repo_path = NULL;
2631 795b88cb 2023-07-10 thomas const char *remote_name = NULL;
2632 795b88cb 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
2633 795b88cb 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
2634 795b88cb 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
2635 795b88cb 2023-07-10 thomas int nremotes;
2636 795b88cb 2023-07-10 thomas char *id_str = NULL;
2637 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
2638 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
2639 795b88cb 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2640 795b88cb 2023-07-10 thomas struct got_pathlist_head paths, refs, symrefs;
2641 795b88cb 2023-07-10 thomas struct got_pathlist_head wanted_branches, wanted_refs;
2642 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
2643 795b88cb 2023-07-10 thomas struct got_reflist_head remote_refs;
2644 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
2645 795b88cb 2023-07-10 thomas struct got_object_id *pack_hash = NULL;
2646 795b88cb 2023-07-10 thomas int i, ch, fetchfd = -1, fetchstatus;
2647 795b88cb 2023-07-10 thomas pid_t fetchpid = -1;
2648 795b88cb 2023-07-10 thomas struct got_fetch_progress_arg fpa;
2649 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
2650 795b88cb 2023-07-10 thomas int verbosity = 0;
2651 795b88cb 2023-07-10 thomas int delete_remote = 0;
2652 795b88cb 2023-07-10 thomas int replace_tags = 0;
2653 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
2654 795b88cb 2023-07-10 thomas const char *remote_head = NULL, *worktree_branch = NULL;
2655 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
2656 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
2657 795b88cb 2023-07-10 thomas const char *refname;
2658 795b88cb 2023-07-10 thomas struct got_reference *head_ref = NULL;
2659 795b88cb 2023-07-10 thomas
2660 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
2661 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2662 795b88cb 2023-07-10 thomas TAILQ_INIT(&symrefs);
2663 795b88cb 2023-07-10 thomas TAILQ_INIT(&remote_refs);
2664 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_branches);
2665 795b88cb 2023-07-10 thomas TAILQ_INIT(&wanted_refs);
2666 795b88cb 2023-07-10 thomas
2667 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:qr:vX")) != -1) {
2668 795b88cb 2023-07-10 thomas switch (ch) {
2669 795b88cb 2023-07-10 thomas case 'c':
2670 795b88cb 2023-07-10 thomas commit_id_str = strdup(optarg);
2671 795b88cb 2023-07-10 thomas if (commit_id_str == NULL)
2672 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
2673 795b88cb 2023-07-10 thomas break;
2674 795b88cb 2023-07-10 thomas case 't':
2675 795b88cb 2023-07-10 thomas replace_tags = 1;
2676 795b88cb 2023-07-10 thomas break;
2677 795b88cb 2023-07-10 thomas case 'q':
2678 795b88cb 2023-07-10 thomas verbosity = -1;
2679 795b88cb 2023-07-10 thomas break;
2680 795b88cb 2023-07-10 thomas case 'r':
2681 795b88cb 2023-07-10 thomas remote_name = optarg;
2682 795b88cb 2023-07-10 thomas break;
2683 795b88cb 2023-07-10 thomas case 'v':
2684 795b88cb 2023-07-10 thomas if (verbosity < 0)
2685 795b88cb 2023-07-10 thomas verbosity = 0;
2686 795b88cb 2023-07-10 thomas else if (verbosity < 3)
2687 795b88cb 2023-07-10 thomas verbosity++;
2688 795b88cb 2023-07-10 thomas break;
2689 795b88cb 2023-07-10 thomas case 'X':
2690 795b88cb 2023-07-10 thomas delete_remote = 1;
2691 795b88cb 2023-07-10 thomas break;
2692 795b88cb 2023-07-10 thomas default:
2693 795b88cb 2023-07-10 thomas usage_update();
2694 795b88cb 2023-07-10 thomas break;
2695 795b88cb 2023-07-10 thomas }
2696 795b88cb 2023-07-10 thomas }
2697 795b88cb 2023-07-10 thomas argc -= optind;
2698 795b88cb 2023-07-10 thomas argv += optind;
2699 795b88cb 2023-07-10 thomas
2700 795b88cb 2023-07-10 thomas if (delete_remote) {
2701 795b88cb 2023-07-10 thomas if (replace_tags)
2702 795b88cb 2023-07-10 thomas option_conflict('X', 't');
2703 795b88cb 2023-07-10 thomas if (remote_name == NULL)
2704 795b88cb 2023-07-10 thomas errx(1, "-X option requires a remote name");
2705 795b88cb 2023-07-10 thomas }
2706 795b88cb 2023-07-10 thomas if (remote_name == NULL)
2707 795b88cb 2023-07-10 thomas remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2708 795b88cb 2023-07-10 thomas
2709 795b88cb 2023-07-10 thomas worktree_path = getcwd(NULL, 0);
2710 795b88cb 2023-07-10 thomas if (worktree_path == NULL) {
2711 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
2712 795b88cb 2023-07-10 thomas goto done;
2713 795b88cb 2023-07-10 thomas }
2714 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, worktree_path, GOT_WORKTREE_CVG_DIR);
2715 795b88cb 2023-07-10 thomas if (error) {
2716 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
2717 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "update",
2718 795b88cb 2023-07-10 thomas worktree_path);
2719 795b88cb 2023-07-10 thomas goto done;
2720 795b88cb 2023-07-10 thomas }
2721 795b88cb 2023-07-10 thomas repo_path = got_worktree_get_repo_path(worktree);
2722 795b88cb 2023-07-10 thomas
2723 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
2724 795b88cb 2023-07-10 thomas if (error != NULL)
2725 795b88cb 2023-07-10 thomas goto done;
2726 795b88cb 2023-07-10 thomas
2727 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2728 795b88cb 2023-07-10 thomas if (error)
2729 795b88cb 2023-07-10 thomas goto done;
2730 795b88cb 2023-07-10 thomas
2731 795b88cb 2023-07-10 thomas error = check_rebase_or_histedit_in_progress(worktree);
2732 795b88cb 2023-07-10 thomas if (error)
2733 795b88cb 2023-07-10 thomas goto done;
2734 795b88cb 2023-07-10 thomas error = check_merge_in_progress(worktree, repo);
2735 795b88cb 2023-07-10 thomas if (error)
2736 795b88cb 2023-07-10 thomas goto done;
2737 795b88cb 2023-07-10 thomas
2738 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
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 worktree_conf = got_worktree_get_gotconfig(worktree);
2743 795b88cb 2023-07-10 thomas if (worktree_conf) {
2744 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2745 795b88cb 2023-07-10 thomas worktree_conf);
2746 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2747 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2748 795b88cb 2023-07-10 thomas remote = &remotes[i];
2749 795b88cb 2023-07-10 thomas break;
2750 795b88cb 2023-07-10 thomas }
2751 795b88cb 2023-07-10 thomas }
2752 795b88cb 2023-07-10 thomas }
2753 795b88cb 2023-07-10 thomas
2754 795b88cb 2023-07-10 thomas if (remote == NULL) {
2755 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
2756 795b88cb 2023-07-10 thomas if (repo_conf) {
2757 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
2758 795b88cb 2023-07-10 thomas repo_conf);
2759 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2760 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2761 795b88cb 2023-07-10 thomas remote = &remotes[i];
2762 795b88cb 2023-07-10 thomas break;
2763 795b88cb 2023-07-10 thomas }
2764 795b88cb 2023-07-10 thomas }
2765 795b88cb 2023-07-10 thomas }
2766 795b88cb 2023-07-10 thomas }
2767 795b88cb 2023-07-10 thomas if (remote == NULL) {
2768 795b88cb 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2769 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
2770 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
2771 795b88cb 2023-07-10 thomas remote = &remotes[i];
2772 795b88cb 2023-07-10 thomas break;
2773 795b88cb 2023-07-10 thomas }
2774 795b88cb 2023-07-10 thomas }
2775 795b88cb 2023-07-10 thomas }
2776 795b88cb 2023-07-10 thomas if (remote == NULL) {
2777 795b88cb 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2778 795b88cb 2023-07-10 thomas goto done;
2779 795b88cb 2023-07-10 thomas }
2780 795b88cb 2023-07-10 thomas
2781 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2782 795b88cb 2023-07-10 thomas &repo_name, remote->fetch_url);
2783 795b88cb 2023-07-10 thomas if (error)
2784 795b88cb 2023-07-10 thomas goto done;
2785 795b88cb 2023-07-10 thomas
2786 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
2787 795b88cb 2023-07-10 thomas #ifndef PROFILE
2788 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2789 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
2790 795b88cb 2023-07-10 thomas err(1, "pledge");
2791 795b88cb 2023-07-10 thomas #endif
2792 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
2793 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
2794 795b88cb 2023-07-10 thomas #ifndef PROFILE
2795 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2796 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
2797 795b88cb 2023-07-10 thomas err(1, "pledge");
2798 795b88cb 2023-07-10 thomas #endif
2799 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
2800 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
2801 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2802 795b88cb 2023-07-10 thomas goto done;
2803 795b88cb 2023-07-10 thomas } else {
2804 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2805 795b88cb 2023-07-10 thomas goto done;
2806 795b88cb 2023-07-10 thomas }
2807 795b88cb 2023-07-10 thomas
2808 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
2809 795b88cb 2023-07-10 thomas if (error)
2810 795b88cb 2023-07-10 thomas goto done;
2811 795b88cb 2023-07-10 thomas
2812 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
2813 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
2814 795b88cb 2023-07-10 thomas if (error)
2815 795b88cb 2023-07-10 thomas goto done;
2816 795b88cb 2023-07-10 thomas
2817 795b88cb 2023-07-10 thomas if (verbosity >= 0) {
2818 795b88cb 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2819 795b88cb 2023-07-10 thomas remote->name, proto, host,
2820 795b88cb 2023-07-10 thomas port ? ":" : "", port ? port : "",
2821 795b88cb 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
2822 795b88cb 2023-07-10 thomas }
2823 795b88cb 2023-07-10 thomas
2824 795b88cb 2023-07-10 thomas error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2825 795b88cb 2023-07-10 thomas server_path, verbosity);
2826 795b88cb 2023-07-10 thomas if (error)
2827 795b88cb 2023-07-10 thomas goto done;
2828 795b88cb 2023-07-10 thomas
2829 795b88cb 2023-07-10 thomas /*
2830 795b88cb 2023-07-10 thomas * If set, get this remote's HEAD ref target so
2831 795b88cb 2023-07-10 thomas * if it has changed on the server we can fetch it.
2832 795b88cb 2023-07-10 thomas */
2833 795b88cb 2023-07-10 thomas error = got_ref_list(&remote_refs, repo, "refs/remotes",
2834 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
2835 795b88cb 2023-07-10 thomas if (error)
2836 795b88cb 2023-07-10 thomas goto done;
2837 795b88cb 2023-07-10 thomas
2838 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &remote_refs, entry) {
2839 795b88cb 2023-07-10 thomas const char *remote_refname, *remote_target;
2840 795b88cb 2023-07-10 thomas size_t remote_name_len;
2841 795b88cb 2023-07-10 thomas
2842 795b88cb 2023-07-10 thomas if (!got_ref_is_symbolic(re->ref))
2843 795b88cb 2023-07-10 thomas continue;
2844 795b88cb 2023-07-10 thomas
2845 795b88cb 2023-07-10 thomas remote_name_len = strlen(remote->name);
2846 795b88cb 2023-07-10 thomas remote_refname = got_ref_get_name(re->ref);
2847 795b88cb 2023-07-10 thomas
2848 795b88cb 2023-07-10 thomas /* we only want refs/remotes/$remote->name/HEAD */
2849 795b88cb 2023-07-10 thomas if (strncmp(remote_refname + 13, remote->name,
2850 795b88cb 2023-07-10 thomas remote_name_len) != 0)
2851 795b88cb 2023-07-10 thomas continue;
2852 795b88cb 2023-07-10 thomas
2853 795b88cb 2023-07-10 thomas if (strcmp(remote_refname + remote_name_len + 14,
2854 795b88cb 2023-07-10 thomas GOT_REF_HEAD) != 0)
2855 795b88cb 2023-07-10 thomas continue;
2856 795b88cb 2023-07-10 thomas
2857 795b88cb 2023-07-10 thomas /*
2858 795b88cb 2023-07-10 thomas * Take the name itself because we already
2859 795b88cb 2023-07-10 thomas * only match with refs/heads/ in fetch_pack().
2860 795b88cb 2023-07-10 thomas */
2861 795b88cb 2023-07-10 thomas remote_target = got_ref_get_symref_target(re->ref);
2862 795b88cb 2023-07-10 thomas remote_head = remote_target + remote_name_len + 14;
2863 795b88cb 2023-07-10 thomas break;
2864 795b88cb 2023-07-10 thomas }
2865 795b88cb 2023-07-10 thomas
2866 795b88cb 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
2867 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
2868 795b88cb 2023-07-10 thomas worktree_branch = refname;
2869 795b88cb 2023-07-10 thomas
2870 795b88cb 2023-07-10 thomas fpa.last_scaled_size[0] = '\0';
2871 795b88cb 2023-07-10 thomas fpa.last_p_indexed = -1;
2872 795b88cb 2023-07-10 thomas fpa.last_p_resolved = -1;
2873 795b88cb 2023-07-10 thomas fpa.verbosity = verbosity;
2874 795b88cb 2023-07-10 thomas fpa.repo = repo;
2875 795b88cb 2023-07-10 thomas fpa.create_configs = 0;
2876 795b88cb 2023-07-10 thomas fpa.configs_created = 0;
2877 795b88cb 2023-07-10 thomas memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2878 795b88cb 2023-07-10 thomas
2879 795b88cb 2023-07-10 thomas error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2880 795b88cb 2023-07-10 thomas remote->mirror_references, 0, &wanted_branches, &wanted_refs,
2881 795b88cb 2023-07-10 thomas 0, verbosity, fetchfd, repo, worktree_branch, remote_head,
2882 795b88cb 2023-07-10 thomas 0, fetch_progress, &fpa);
2883 795b88cb 2023-07-10 thomas if (error)
2884 795b88cb 2023-07-10 thomas goto done;
2885 795b88cb 2023-07-10 thomas
2886 795b88cb 2023-07-10 thomas if (pack_hash != NULL && verbosity >= 0) {
2887 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, pack_hash);
2888 795b88cb 2023-07-10 thomas if (error)
2889 795b88cb 2023-07-10 thomas goto done;
2890 795b88cb 2023-07-10 thomas printf("\nFetched %s.pack\n", id_str);
2891 795b88cb 2023-07-10 thomas free(id_str);
2892 795b88cb 2023-07-10 thomas id_str = NULL;
2893 795b88cb 2023-07-10 thomas }
2894 795b88cb 2023-07-10 thomas
2895 795b88cb 2023-07-10 thomas /* Update references provided with the pack file. */
2896 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &refs, entry) {
2897 795b88cb 2023-07-10 thomas const char *refname = pe->path;
2898 795b88cb 2023-07-10 thomas struct got_object_id *id = pe->data;
2899 795b88cb 2023-07-10 thomas struct got_reference *ref;
2900 795b88cb 2023-07-10 thomas
2901 795b88cb 2023-07-10 thomas if (is_wanted_ref(&wanted_refs, refname)) {
2902 795b88cb 2023-07-10 thomas error = update_wanted_ref(refname, id,
2903 795b88cb 2023-07-10 thomas remote->name, verbosity, repo);
2904 795b88cb 2023-07-10 thomas if (error)
2905 795b88cb 2023-07-10 thomas goto done;
2906 795b88cb 2023-07-10 thomas continue;
2907 795b88cb 2023-07-10 thomas }
2908 795b88cb 2023-07-10 thomas
2909 795b88cb 2023-07-10 thomas error = got_ref_open(&ref, repo, refname, 1);
2910 795b88cb 2023-07-10 thomas if (error) {
2911 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF)
2912 795b88cb 2023-07-10 thomas goto done;
2913 795b88cb 2023-07-10 thomas error = create_ref(refname, id, verbosity,
2914 795b88cb 2023-07-10 thomas repo);
2915 795b88cb 2023-07-10 thomas if (error)
2916 795b88cb 2023-07-10 thomas goto done;
2917 795b88cb 2023-07-10 thomas } else {
2918 795b88cb 2023-07-10 thomas error = update_ref(ref, id, replace_tags,
2919 795b88cb 2023-07-10 thomas verbosity-1, repo);
2920 795b88cb 2023-07-10 thomas unlock_err = got_ref_unlock(ref);
2921 795b88cb 2023-07-10 thomas if (unlock_err && error == NULL)
2922 795b88cb 2023-07-10 thomas error = unlock_err;
2923 795b88cb 2023-07-10 thomas got_ref_close(ref);
2924 795b88cb 2023-07-10 thomas if (error)
2925 795b88cb 2023-07-10 thomas goto done;
2926 795b88cb 2023-07-10 thomas }
2927 795b88cb 2023-07-10 thomas }
2928 795b88cb 2023-07-10 thomas
2929 795b88cb 2023-07-10 thomas /* Update worktree */
2930 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
2931 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), 0);
2932 795b88cb 2023-07-10 thomas if (error != NULL)
2933 795b88cb 2023-07-10 thomas goto done;
2934 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
2935 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
2936 795b88cb 2023-07-10 thomas if (error != NULL)
2937 795b88cb 2023-07-10 thomas goto done;
2938 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2939 795b88cb 2023-07-10 thomas if (error != NULL)
2940 795b88cb 2023-07-10 thomas goto done;
2941 795b88cb 2023-07-10 thomas } else {
2942 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
2943 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
2944 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2945 795b88cb 2023-07-10 thomas NULL);
2946 795b88cb 2023-07-10 thomas if (error)
2947 795b88cb 2023-07-10 thomas goto done;
2948 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
2949 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2950 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
2951 795b88cb 2023-07-10 thomas free(commit_id_str);
2952 795b88cb 2023-07-10 thomas commit_id_str = NULL;
2953 795b88cb 2023-07-10 thomas if (error)
2954 795b88cb 2023-07-10 thomas goto done;
2955 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
2956 795b88cb 2023-07-10 thomas if (error)
2957 795b88cb 2023-07-10 thomas goto done;
2958 795b88cb 2023-07-10 thomas }
2959 795b88cb 2023-07-10 thomas
2960 795b88cb 2023-07-10 thomas
2961 795b88cb 2023-07-10 thomas error = check_linear_ancestry(commit_id,
2962 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree), 0, repo);
2963 795b88cb 2023-07-10 thomas if (error != NULL) {
2964 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_ANCESTRY)
2965 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_BRANCH_MOVED);
2966 795b88cb 2023-07-10 thomas goto done;
2967 795b88cb 2023-07-10 thomas }
2968 795b88cb 2023-07-10 thomas error = check_same_branch(commit_id, head_ref, repo);
2969 795b88cb 2023-07-10 thomas if (error)
2970 795b88cb 2023-07-10 thomas goto done;
2971 795b88cb 2023-07-10 thomas
2972 795b88cb 2023-07-10 thomas if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2973 795b88cb 2023-07-10 thomas commit_id) != 0) {
2974 795b88cb 2023-07-10 thomas error = got_worktree_set_base_commit_id(worktree, repo,
2975 795b88cb 2023-07-10 thomas commit_id);
2976 795b88cb 2023-07-10 thomas if (error)
2977 795b88cb 2023-07-10 thomas goto done;
2978 795b88cb 2023-07-10 thomas }
2979 795b88cb 2023-07-10 thomas
2980 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
2981 795b88cb 2023-07-10 thomas upa.verbosity = verbosity;
2982 795b88cb 2023-07-10 thomas error = got_worktree_checkout_files(worktree, &paths, repo,
2983 795b88cb 2023-07-10 thomas update_progress, &upa, check_cancelled, NULL);
2984 795b88cb 2023-07-10 thomas if (error != NULL)
2985 795b88cb 2023-07-10 thomas goto done;
2986 795b88cb 2023-07-10 thomas
2987 795b88cb 2023-07-10 thomas if (upa.did_something) {
2988 795b88cb 2023-07-10 thomas printf("Updated to %s: %s\n",
2989 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree), commit_id_str);
2990 795b88cb 2023-07-10 thomas } else
2991 795b88cb 2023-07-10 thomas printf("Already up-to-date\n");
2992 795b88cb 2023-07-10 thomas
2993 795b88cb 2023-07-10 thomas print_update_progress_stats(&upa);
2994 795b88cb 2023-07-10 thomas done:
2995 795b88cb 2023-07-10 thomas if (fetchpid > 0) {
2996 795b88cb 2023-07-10 thomas if (kill(fetchpid, SIGTERM) == -1)
2997 795b88cb 2023-07-10 thomas error = got_error_from_errno("kill");
2998 795b88cb 2023-07-10 thomas if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2999 795b88cb 2023-07-10 thomas error = got_error_from_errno("waitpid");
3000 795b88cb 2023-07-10 thomas }
3001 795b88cb 2023-07-10 thomas if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
3002 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
3003 795b88cb 2023-07-10 thomas if (repo) {
3004 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
3005 795b88cb 2023-07-10 thomas if (error == NULL)
3006 795b88cb 2023-07-10 thomas error = close_err;
3007 795b88cb 2023-07-10 thomas }
3008 795b88cb 2023-07-10 thomas if (worktree)
3009 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
3010 795b88cb 2023-07-10 thomas if (pack_fds) {
3011 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
3012 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
3013 795b88cb 2023-07-10 thomas if (error == NULL)
3014 795b88cb 2023-07-10 thomas error = pack_err;
3015 795b88cb 2023-07-10 thomas }
3016 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3017 795b88cb 2023-07-10 thomas got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
3018 795b88cb 2023-07-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
3019 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
3020 795b88cb 2023-07-10 thomas got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
3021 795b88cb 2023-07-10 thomas got_ref_list_free(&remote_refs);
3022 795b88cb 2023-07-10 thomas free(id_str);
3023 795b88cb 2023-07-10 thomas free(worktree_path);
3024 795b88cb 2023-07-10 thomas free(pack_hash);
3025 795b88cb 2023-07-10 thomas free(proto);
3026 795b88cb 2023-07-10 thomas free(host);
3027 795b88cb 2023-07-10 thomas free(port);
3028 795b88cb 2023-07-10 thomas free(server_path);
3029 795b88cb 2023-07-10 thomas free(repo_name);
3030 795b88cb 2023-07-10 thomas free(commit_id);
3031 795b88cb 2023-07-10 thomas free(commit_id_str);
3032 795b88cb 2023-07-10 thomas return error;
3033 795b88cb 2023-07-10 thomas }
3034 795b88cb 2023-07-10 thomas
3035 795b88cb 2023-07-10 thomas static const struct got_error *
3036 795b88cb 2023-07-10 thomas diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3037 795b88cb 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3038 795b88cb 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3039 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3040 795b88cb 2023-07-10 thomas {
3041 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3042 795b88cb 2023-07-10 thomas struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3043 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3044 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3045 795b88cb 2023-07-10 thomas
3046 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3047 795b88cb 2023-07-10 thomas if (fd1 == -1)
3048 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
3049 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3050 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3051 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3052 795b88cb 2023-07-10 thomas goto done;
3053 795b88cb 2023-07-10 thomas }
3054 795b88cb 2023-07-10 thomas
3055 795b88cb 2023-07-10 thomas if (blob_id1) {
3056 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3057 795b88cb 2023-07-10 thomas fd1);
3058 795b88cb 2023-07-10 thomas if (err)
3059 795b88cb 2023-07-10 thomas goto done;
3060 795b88cb 2023-07-10 thomas }
3061 795b88cb 2023-07-10 thomas
3062 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3063 795b88cb 2023-07-10 thomas if (err)
3064 795b88cb 2023-07-10 thomas goto done;
3065 795b88cb 2023-07-10 thomas
3066 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3067 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3068 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3069 795b88cb 2023-07-10 thomas goto done;
3070 795b88cb 2023-07-10 thomas }
3071 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3072 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3073 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3074 795b88cb 2023-07-10 thomas goto done;
3075 795b88cb 2023-07-10 thomas }
3076 795b88cb 2023-07-10 thomas
3077 795b88cb 2023-07-10 thomas while (path[0] == '/')
3078 795b88cb 2023-07-10 thomas path++;
3079 795b88cb 2023-07-10 thomas err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3080 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3081 795b88cb 2023-07-10 thomas force_text_diff, dsa, outfile);
3082 795b88cb 2023-07-10 thomas done:
3083 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3084 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3085 795b88cb 2023-07-10 thomas if (blob1)
3086 795b88cb 2023-07-10 thomas got_object_blob_close(blob1);
3087 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3088 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3089 795b88cb 2023-07-10 thomas if (blob2)
3090 795b88cb 2023-07-10 thomas got_object_blob_close(blob2);
3091 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3092 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3093 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3094 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3095 795b88cb 2023-07-10 thomas return err;
3096 795b88cb 2023-07-10 thomas }
3097 795b88cb 2023-07-10 thomas
3098 795b88cb 2023-07-10 thomas static const struct got_error *
3099 795b88cb 2023-07-10 thomas diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3100 795b88cb 2023-07-10 thomas const char *path, int diff_context, int ignore_whitespace,
3101 795b88cb 2023-07-10 thomas int force_text_diff, struct got_diffstat_cb_arg *dsa,
3102 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3103 795b88cb 2023-07-10 thomas {
3104 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3105 795b88cb 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3106 795b88cb 2023-07-10 thomas struct got_diff_blob_output_unidiff_arg arg;
3107 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3108 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3109 795b88cb 2023-07-10 thomas
3110 795b88cb 2023-07-10 thomas if (tree_id1) {
3111 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3112 795b88cb 2023-07-10 thomas if (err)
3113 795b88cb 2023-07-10 thomas goto done;
3114 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3115 795b88cb 2023-07-10 thomas if (fd1 == -1) {
3116 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3117 795b88cb 2023-07-10 thomas goto done;
3118 795b88cb 2023-07-10 thomas }
3119 795b88cb 2023-07-10 thomas }
3120 795b88cb 2023-07-10 thomas
3121 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3122 795b88cb 2023-07-10 thomas if (err)
3123 795b88cb 2023-07-10 thomas goto done;
3124 795b88cb 2023-07-10 thomas
3125 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3126 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3127 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3128 795b88cb 2023-07-10 thomas goto done;
3129 795b88cb 2023-07-10 thomas }
3130 795b88cb 2023-07-10 thomas
3131 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3132 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3133 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3134 795b88cb 2023-07-10 thomas goto done;
3135 795b88cb 2023-07-10 thomas }
3136 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3137 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3138 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3139 795b88cb 2023-07-10 thomas goto done;
3140 795b88cb 2023-07-10 thomas }
3141 795b88cb 2023-07-10 thomas arg.diff_context = diff_context;
3142 795b88cb 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
3143 795b88cb 2023-07-10 thomas arg.force_text_diff = force_text_diff;
3144 795b88cb 2023-07-10 thomas arg.diffstat = dsa;
3145 795b88cb 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3146 795b88cb 2023-07-10 thomas arg.outfile = outfile;
3147 795b88cb 2023-07-10 thomas arg.lines = NULL;
3148 795b88cb 2023-07-10 thomas arg.nlines = 0;
3149 795b88cb 2023-07-10 thomas while (path[0] == '/')
3150 795b88cb 2023-07-10 thomas path++;
3151 795b88cb 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3152 795b88cb 2023-07-10 thomas got_diff_blob_output_unidiff, &arg, 1);
3153 795b88cb 2023-07-10 thomas done:
3154 795b88cb 2023-07-10 thomas if (tree1)
3155 795b88cb 2023-07-10 thomas got_object_tree_close(tree1);
3156 795b88cb 2023-07-10 thomas if (tree2)
3157 795b88cb 2023-07-10 thomas got_object_tree_close(tree2);
3158 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3159 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3160 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3161 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3162 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3163 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3164 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3165 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3166 795b88cb 2023-07-10 thomas return err;
3167 795b88cb 2023-07-10 thomas }
3168 795b88cb 2023-07-10 thomas
3169 795b88cb 2023-07-10 thomas static const struct got_error *
3170 795b88cb 2023-07-10 thomas get_changed_paths(struct got_pathlist_head *paths,
3171 795b88cb 2023-07-10 thomas struct got_commit_object *commit, struct got_repository *repo,
3172 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *dsa)
3173 795b88cb 2023-07-10 thomas {
3174 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3175 795b88cb 2023-07-10 thomas struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3176 795b88cb 2023-07-10 thomas struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3177 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3178 795b88cb 2023-07-10 thomas got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3179 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
3180 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
3181 795b88cb 2023-07-10 thomas
3182 795b88cb 2023-07-10 thomas if (dsa) {
3183 795b88cb 2023-07-10 thomas cb = got_diff_tree_compute_diffstat;
3184 795b88cb 2023-07-10 thomas
3185 795b88cb 2023-07-10 thomas f1 = got_opentemp();
3186 795b88cb 2023-07-10 thomas if (f1 == NULL) {
3187 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3188 795b88cb 2023-07-10 thomas goto done;
3189 795b88cb 2023-07-10 thomas }
3190 795b88cb 2023-07-10 thomas f2 = got_opentemp();
3191 795b88cb 2023-07-10 thomas if (f2 == NULL) {
3192 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3193 795b88cb 2023-07-10 thomas goto done;
3194 795b88cb 2023-07-10 thomas }
3195 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
3196 795b88cb 2023-07-10 thomas if (fd1 == -1) {
3197 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3198 795b88cb 2023-07-10 thomas goto done;
3199 795b88cb 2023-07-10 thomas }
3200 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
3201 795b88cb 2023-07-10 thomas if (fd2 == -1) {
3202 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
3203 795b88cb 2023-07-10 thomas goto done;
3204 795b88cb 2023-07-10 thomas }
3205 795b88cb 2023-07-10 thomas }
3206 795b88cb 2023-07-10 thomas
3207 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3208 795b88cb 2023-07-10 thomas if (qid != NULL) {
3209 795b88cb 2023-07-10 thomas struct got_commit_object *pcommit;
3210 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3211 795b88cb 2023-07-10 thomas &qid->id);
3212 795b88cb 2023-07-10 thomas if (err)
3213 795b88cb 2023-07-10 thomas return err;
3214 795b88cb 2023-07-10 thomas
3215 795b88cb 2023-07-10 thomas tree_id1 = got_object_id_dup(
3216 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
3217 795b88cb 2023-07-10 thomas if (tree_id1 == NULL) {
3218 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3219 795b88cb 2023-07-10 thomas return got_error_from_errno("got_object_id_dup");
3220 795b88cb 2023-07-10 thomas }
3221 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3222 795b88cb 2023-07-10 thomas
3223 795b88cb 2023-07-10 thomas }
3224 795b88cb 2023-07-10 thomas
3225 795b88cb 2023-07-10 thomas if (tree_id1) {
3226 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree1, repo, tree_id1);
3227 795b88cb 2023-07-10 thomas if (err)
3228 795b88cb 2023-07-10 thomas goto done;
3229 795b88cb 2023-07-10 thomas }
3230 795b88cb 2023-07-10 thomas
3231 795b88cb 2023-07-10 thomas tree_id2 = got_object_commit_get_tree_id(commit);
3232 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree2, repo, tree_id2);
3233 795b88cb 2023-07-10 thomas if (err)
3234 795b88cb 2023-07-10 thomas goto done;
3235 795b88cb 2023-07-10 thomas
3236 795b88cb 2023-07-10 thomas err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3237 795b88cb 2023-07-10 thomas cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3238 795b88cb 2023-07-10 thomas done:
3239 795b88cb 2023-07-10 thomas if (tree1)
3240 795b88cb 2023-07-10 thomas got_object_tree_close(tree1);
3241 795b88cb 2023-07-10 thomas if (tree2)
3242 795b88cb 2023-07-10 thomas got_object_tree_close(tree2);
3243 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3244 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3245 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3246 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
3247 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && err == NULL)
3248 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3249 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
3250 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3251 795b88cb 2023-07-10 thomas free(tree_id1);
3252 795b88cb 2023-07-10 thomas return err;
3253 795b88cb 2023-07-10 thomas }
3254 795b88cb 2023-07-10 thomas
3255 795b88cb 2023-07-10 thomas static const struct got_error *
3256 795b88cb 2023-07-10 thomas print_patch(struct got_commit_object *commit, struct got_object_id *id,
3257 795b88cb 2023-07-10 thomas const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3258 795b88cb 2023-07-10 thomas struct got_repository *repo, FILE *outfile)
3259 795b88cb 2023-07-10 thomas {
3260 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3261 795b88cb 2023-07-10 thomas struct got_commit_object *pcommit = NULL;
3262 795b88cb 2023-07-10 thomas char *id_str1 = NULL, *id_str2 = NULL;
3263 795b88cb 2023-07-10 thomas struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3264 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3265 795b88cb 2023-07-10 thomas
3266 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3267 795b88cb 2023-07-10 thomas if (qid != NULL) {
3268 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo,
3269 795b88cb 2023-07-10 thomas &qid->id);
3270 795b88cb 2023-07-10 thomas if (err)
3271 795b88cb 2023-07-10 thomas return err;
3272 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str1, &qid->id);
3273 795b88cb 2023-07-10 thomas if (err)
3274 795b88cb 2023-07-10 thomas goto done;
3275 795b88cb 2023-07-10 thomas }
3276 795b88cb 2023-07-10 thomas
3277 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str2, id);
3278 795b88cb 2023-07-10 thomas if (err)
3279 795b88cb 2023-07-10 thomas goto done;
3280 795b88cb 2023-07-10 thomas
3281 795b88cb 2023-07-10 thomas if (path && path[0] != '\0') {
3282 795b88cb 2023-07-10 thomas int obj_type;
3283 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&obj_id2, repo, commit, path);
3284 795b88cb 2023-07-10 thomas if (err)
3285 795b88cb 2023-07-10 thomas goto done;
3286 795b88cb 2023-07-10 thomas if (pcommit) {
3287 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&obj_id1, repo,
3288 795b88cb 2023-07-10 thomas pcommit, path);
3289 795b88cb 2023-07-10 thomas if (err) {
3290 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3291 795b88cb 2023-07-10 thomas free(obj_id2);
3292 795b88cb 2023-07-10 thomas goto done;
3293 795b88cb 2023-07-10 thomas }
3294 795b88cb 2023-07-10 thomas }
3295 795b88cb 2023-07-10 thomas }
3296 795b88cb 2023-07-10 thomas err = got_object_get_type(&obj_type, repo, obj_id2);
3297 795b88cb 2023-07-10 thomas if (err) {
3298 795b88cb 2023-07-10 thomas free(obj_id2);
3299 795b88cb 2023-07-10 thomas goto done;
3300 795b88cb 2023-07-10 thomas }
3301 795b88cb 2023-07-10 thomas fprintf(outfile,
3302 795b88cb 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3303 795b88cb 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3304 795b88cb 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3305 795b88cb 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3306 795b88cb 2023-07-10 thomas switch (obj_type) {
3307 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
3308 795b88cb 2023-07-10 thomas err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3309 795b88cb 2023-07-10 thomas 0, 0, dsa, repo, outfile);
3310 795b88cb 2023-07-10 thomas break;
3311 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
3312 795b88cb 2023-07-10 thomas err = diff_trees(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 default:
3316 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_OBJ_TYPE);
3317 795b88cb 2023-07-10 thomas break;
3318 795b88cb 2023-07-10 thomas }
3319 795b88cb 2023-07-10 thomas free(obj_id1);
3320 795b88cb 2023-07-10 thomas free(obj_id2);
3321 795b88cb 2023-07-10 thomas } else {
3322 795b88cb 2023-07-10 thomas obj_id2 = got_object_commit_get_tree_id(commit);
3323 795b88cb 2023-07-10 thomas if (pcommit)
3324 795b88cb 2023-07-10 thomas obj_id1 = got_object_commit_get_tree_id(pcommit);
3325 795b88cb 2023-07-10 thomas fprintf(outfile,
3326 795b88cb 2023-07-10 thomas "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3327 795b88cb 2023-07-10 thomas fprintf(outfile, "commit - %s\n",
3328 795b88cb 2023-07-10 thomas id_str1 ? id_str1 : "/dev/null");
3329 795b88cb 2023-07-10 thomas fprintf(outfile, "commit + %s\n", id_str2);
3330 795b88cb 2023-07-10 thomas err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3331 795b88cb 2023-07-10 thomas dsa, repo, outfile);
3332 795b88cb 2023-07-10 thomas }
3333 795b88cb 2023-07-10 thomas done:
3334 795b88cb 2023-07-10 thomas free(id_str1);
3335 795b88cb 2023-07-10 thomas free(id_str2);
3336 795b88cb 2023-07-10 thomas if (pcommit)
3337 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
3338 795b88cb 2023-07-10 thomas return err;
3339 795b88cb 2023-07-10 thomas }
3340 795b88cb 2023-07-10 thomas
3341 795b88cb 2023-07-10 thomas static char *
3342 795b88cb 2023-07-10 thomas get_datestr(time_t *time, char *datebuf)
3343 795b88cb 2023-07-10 thomas {
3344 795b88cb 2023-07-10 thomas struct tm mytm, *tm;
3345 795b88cb 2023-07-10 thomas char *p, *s;
3346 795b88cb 2023-07-10 thomas
3347 795b88cb 2023-07-10 thomas tm = gmtime_r(time, &mytm);
3348 795b88cb 2023-07-10 thomas if (tm == NULL)
3349 795b88cb 2023-07-10 thomas return NULL;
3350 795b88cb 2023-07-10 thomas s = asctime_r(tm, datebuf);
3351 795b88cb 2023-07-10 thomas if (s == NULL)
3352 795b88cb 2023-07-10 thomas return NULL;
3353 795b88cb 2023-07-10 thomas p = strchr(s, '\n');
3354 795b88cb 2023-07-10 thomas if (p)
3355 795b88cb 2023-07-10 thomas *p = '\0';
3356 795b88cb 2023-07-10 thomas return s;
3357 795b88cb 2023-07-10 thomas }
3358 795b88cb 2023-07-10 thomas
3359 795b88cb 2023-07-10 thomas static const struct got_error *
3360 795b88cb 2023-07-10 thomas match_commit(int *have_match, struct got_object_id *id,
3361 795b88cb 2023-07-10 thomas struct got_commit_object *commit, regex_t *regex)
3362 795b88cb 2023-07-10 thomas {
3363 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3364 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3365 795b88cb 2023-07-10 thomas char *id_str = NULL, *logmsg = NULL;
3366 795b88cb 2023-07-10 thomas
3367 795b88cb 2023-07-10 thomas *have_match = 0;
3368 795b88cb 2023-07-10 thomas
3369 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3370 795b88cb 2023-07-10 thomas if (err)
3371 795b88cb 2023-07-10 thomas return err;
3372 795b88cb 2023-07-10 thomas
3373 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
3374 795b88cb 2023-07-10 thomas if (err)
3375 795b88cb 2023-07-10 thomas goto done;
3376 795b88cb 2023-07-10 thomas
3377 795b88cb 2023-07-10 thomas if (regexec(regex, got_object_commit_get_author(commit), 1,
3378 795b88cb 2023-07-10 thomas &regmatch, 0) == 0 ||
3379 795b88cb 2023-07-10 thomas regexec(regex, got_object_commit_get_committer(commit), 1,
3380 795b88cb 2023-07-10 thomas &regmatch, 0) == 0 ||
3381 795b88cb 2023-07-10 thomas regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
3382 795b88cb 2023-07-10 thomas regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3383 795b88cb 2023-07-10 thomas *have_match = 1;
3384 795b88cb 2023-07-10 thomas done:
3385 795b88cb 2023-07-10 thomas free(id_str);
3386 795b88cb 2023-07-10 thomas free(logmsg);
3387 795b88cb 2023-07-10 thomas return err;
3388 795b88cb 2023-07-10 thomas }
3389 795b88cb 2023-07-10 thomas
3390 795b88cb 2023-07-10 thomas static void
3391 795b88cb 2023-07-10 thomas match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3392 795b88cb 2023-07-10 thomas regex_t *regex)
3393 795b88cb 2023-07-10 thomas {
3394 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3395 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3396 795b88cb 2023-07-10 thomas
3397 795b88cb 2023-07-10 thomas *have_match = 0;
3398 795b88cb 2023-07-10 thomas
3399 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
3400 795b88cb 2023-07-10 thomas if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3401 795b88cb 2023-07-10 thomas *have_match = 1;
3402 795b88cb 2023-07-10 thomas break;
3403 795b88cb 2023-07-10 thomas }
3404 795b88cb 2023-07-10 thomas }
3405 795b88cb 2023-07-10 thomas }
3406 795b88cb 2023-07-10 thomas
3407 795b88cb 2023-07-10 thomas static const struct got_error *
3408 795b88cb 2023-07-10 thomas match_patch(int *have_match, struct got_commit_object *commit,
3409 795b88cb 2023-07-10 thomas struct got_object_id *id, const char *path, int diff_context,
3410 795b88cb 2023-07-10 thomas struct got_repository *repo, regex_t *regex, FILE *f)
3411 795b88cb 2023-07-10 thomas {
3412 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3413 795b88cb 2023-07-10 thomas char *line = NULL;
3414 795b88cb 2023-07-10 thomas size_t linesize = 0;
3415 795b88cb 2023-07-10 thomas regmatch_t regmatch;
3416 795b88cb 2023-07-10 thomas
3417 795b88cb 2023-07-10 thomas *have_match = 0;
3418 795b88cb 2023-07-10 thomas
3419 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(f);
3420 795b88cb 2023-07-10 thomas if (err)
3421 795b88cb 2023-07-10 thomas return err;
3422 795b88cb 2023-07-10 thomas
3423 795b88cb 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3424 795b88cb 2023-07-10 thomas if (err)
3425 795b88cb 2023-07-10 thomas goto done;
3426 795b88cb 2023-07-10 thomas
3427 795b88cb 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1) {
3428 795b88cb 2023-07-10 thomas err = got_error_from_errno("fseeko");
3429 795b88cb 2023-07-10 thomas goto done;
3430 795b88cb 2023-07-10 thomas }
3431 795b88cb 2023-07-10 thomas
3432 795b88cb 2023-07-10 thomas while (getline(&line, &linesize, f) != -1) {
3433 795b88cb 2023-07-10 thomas if (regexec(regex, line, 1, &regmatch, 0) == 0) {
3434 795b88cb 2023-07-10 thomas *have_match = 1;
3435 795b88cb 2023-07-10 thomas break;
3436 795b88cb 2023-07-10 thomas }
3437 795b88cb 2023-07-10 thomas }
3438 795b88cb 2023-07-10 thomas done:
3439 795b88cb 2023-07-10 thomas free(line);
3440 795b88cb 2023-07-10 thomas return err;
3441 795b88cb 2023-07-10 thomas }
3442 795b88cb 2023-07-10 thomas
3443 795b88cb 2023-07-10 thomas #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3444 795b88cb 2023-07-10 thomas
3445 795b88cb 2023-07-10 thomas static const struct got_error*
3446 795b88cb 2023-07-10 thomas build_refs_str(char **refs_str, struct got_reflist_head *refs,
3447 795b88cb 2023-07-10 thomas struct got_object_id *id, struct got_repository *repo,
3448 795b88cb 2023-07-10 thomas int local_only)
3449 795b88cb 2023-07-10 thomas {
3450 795b88cb 2023-07-10 thomas static const struct got_error *err = NULL;
3451 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
3452 795b88cb 2023-07-10 thomas char *s;
3453 795b88cb 2023-07-10 thomas const char *name;
3454 795b88cb 2023-07-10 thomas
3455 795b88cb 2023-07-10 thomas *refs_str = NULL;
3456 795b88cb 2023-07-10 thomas
3457 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, refs, entry) {
3458 795b88cb 2023-07-10 thomas struct got_tag_object *tag = NULL;
3459 795b88cb 2023-07-10 thomas struct got_object_id *ref_id;
3460 795b88cb 2023-07-10 thomas int cmp;
3461 795b88cb 2023-07-10 thomas
3462 795b88cb 2023-07-10 thomas name = got_ref_get_name(re->ref);
3463 795b88cb 2023-07-10 thomas if (strcmp(name, GOT_REF_HEAD) == 0)
3464 795b88cb 2023-07-10 thomas continue;
3465 795b88cb 2023-07-10 thomas if (strncmp(name, "refs/", 5) == 0)
3466 795b88cb 2023-07-10 thomas name += 5;
3467 795b88cb 2023-07-10 thomas if (strncmp(name, "got/", 4) == 0)
3468 795b88cb 2023-07-10 thomas continue;
3469 795b88cb 2023-07-10 thomas if (strncmp(name, "heads/", 6) == 0)
3470 795b88cb 2023-07-10 thomas name += 6;
3471 795b88cb 2023-07-10 thomas if (strncmp(name, "remotes/", 8) == 0) {
3472 795b88cb 2023-07-10 thomas if (local_only)
3473 795b88cb 2023-07-10 thomas continue;
3474 795b88cb 2023-07-10 thomas name += 8;
3475 795b88cb 2023-07-10 thomas s = strstr(name, "/" GOT_REF_HEAD);
3476 795b88cb 2023-07-10 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
3477 795b88cb 2023-07-10 thomas continue;
3478 795b88cb 2023-07-10 thomas }
3479 795b88cb 2023-07-10 thomas err = got_ref_resolve(&ref_id, repo, re->ref);
3480 795b88cb 2023-07-10 thomas if (err)
3481 795b88cb 2023-07-10 thomas break;
3482 795b88cb 2023-07-10 thomas if (strncmp(name, "tags/", 5) == 0) {
3483 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, ref_id);
3484 795b88cb 2023-07-10 thomas if (err) {
3485 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
3486 795b88cb 2023-07-10 thomas free(ref_id);
3487 795b88cb 2023-07-10 thomas break;
3488 795b88cb 2023-07-10 thomas }
3489 795b88cb 2023-07-10 thomas /* Ref points at something other than a tag. */
3490 795b88cb 2023-07-10 thomas err = NULL;
3491 795b88cb 2023-07-10 thomas tag = NULL;
3492 795b88cb 2023-07-10 thomas }
3493 795b88cb 2023-07-10 thomas }
3494 795b88cb 2023-07-10 thomas cmp = got_object_id_cmp(tag ?
3495 795b88cb 2023-07-10 thomas got_object_tag_get_object_id(tag) : ref_id, id);
3496 795b88cb 2023-07-10 thomas free(ref_id);
3497 795b88cb 2023-07-10 thomas if (tag)
3498 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
3499 795b88cb 2023-07-10 thomas if (cmp != 0)
3500 795b88cb 2023-07-10 thomas continue;
3501 795b88cb 2023-07-10 thomas s = *refs_str;
3502 795b88cb 2023-07-10 thomas if (asprintf(refs_str, "%s%s%s", s ? s : "",
3503 795b88cb 2023-07-10 thomas s ? ", " : "", name) == -1) {
3504 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
3505 795b88cb 2023-07-10 thomas free(s);
3506 795b88cb 2023-07-10 thomas *refs_str = NULL;
3507 795b88cb 2023-07-10 thomas break;
3508 795b88cb 2023-07-10 thomas }
3509 795b88cb 2023-07-10 thomas free(s);
3510 795b88cb 2023-07-10 thomas }
3511 795b88cb 2023-07-10 thomas
3512 795b88cb 2023-07-10 thomas return err;
3513 795b88cb 2023-07-10 thomas }
3514 795b88cb 2023-07-10 thomas
3515 795b88cb 2023-07-10 thomas static const struct got_error *
3516 795b88cb 2023-07-10 thomas print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3517 795b88cb 2023-07-10 thomas struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3518 795b88cb 2023-07-10 thomas {
3519 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3520 795b88cb 2023-07-10 thomas char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3521 795b88cb 2023-07-10 thomas char *comma, *s, *nl;
3522 795b88cb 2023-07-10 thomas struct got_reflist_head *refs;
3523 795b88cb 2023-07-10 thomas char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3524 795b88cb 2023-07-10 thomas struct tm tm;
3525 795b88cb 2023-07-10 thomas time_t committer_time;
3526 795b88cb 2023-07-10 thomas
3527 795b88cb 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3528 795b88cb 2023-07-10 thomas if (refs) {
3529 795b88cb 2023-07-10 thomas err = build_refs_str(&ref_str, refs, id, repo, 1);
3530 795b88cb 2023-07-10 thomas if (err)
3531 795b88cb 2023-07-10 thomas return err;
3532 795b88cb 2023-07-10 thomas
3533 795b88cb 2023-07-10 thomas /* Display the first matching ref only. */
3534 795b88cb 2023-07-10 thomas if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3535 795b88cb 2023-07-10 thomas *comma = '\0';
3536 795b88cb 2023-07-10 thomas }
3537 795b88cb 2023-07-10 thomas
3538 795b88cb 2023-07-10 thomas if (ref_str == NULL) {
3539 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3540 795b88cb 2023-07-10 thomas if (err)
3541 795b88cb 2023-07-10 thomas return err;
3542 795b88cb 2023-07-10 thomas }
3543 795b88cb 2023-07-10 thomas
3544 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
3545 795b88cb 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL) {
3546 795b88cb 2023-07-10 thomas err = got_error_from_errno("gmtime_r");
3547 795b88cb 2023-07-10 thomas goto done;
3548 795b88cb 2023-07-10 thomas }
3549 795b88cb 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
3550 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
3551 795b88cb 2023-07-10 thomas goto done;
3552 795b88cb 2023-07-10 thomas }
3553 795b88cb 2023-07-10 thomas
3554 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
3555 795b88cb 2023-07-10 thomas if (err)
3556 795b88cb 2023-07-10 thomas goto done;
3557 795b88cb 2023-07-10 thomas
3558 795b88cb 2023-07-10 thomas s = logmsg0;
3559 795b88cb 2023-07-10 thomas while (isspace((unsigned char)s[0]))
3560 795b88cb 2023-07-10 thomas s++;
3561 795b88cb 2023-07-10 thomas
3562 795b88cb 2023-07-10 thomas nl = strchr(s, '\n');
3563 795b88cb 2023-07-10 thomas if (nl) {
3564 795b88cb 2023-07-10 thomas *nl = '\0';
3565 795b88cb 2023-07-10 thomas }
3566 795b88cb 2023-07-10 thomas
3567 795b88cb 2023-07-10 thomas if (ref_str)
3568 795b88cb 2023-07-10 thomas printf("%s%-7s %s\n", datebuf, ref_str, s);
3569 795b88cb 2023-07-10 thomas else
3570 795b88cb 2023-07-10 thomas printf("%s%.7s %s\n", datebuf, id_str, s);
3571 795b88cb 2023-07-10 thomas
3572 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
3573 795b88cb 2023-07-10 thomas err = got_error_from_errno("fflush");
3574 795b88cb 2023-07-10 thomas done:
3575 795b88cb 2023-07-10 thomas free(id_str);
3576 795b88cb 2023-07-10 thomas free(ref_str);
3577 795b88cb 2023-07-10 thomas free(logmsg0);
3578 795b88cb 2023-07-10 thomas return err;
3579 795b88cb 2023-07-10 thomas }
3580 795b88cb 2023-07-10 thomas
3581 795b88cb 2023-07-10 thomas static const struct got_error *
3582 795b88cb 2023-07-10 thomas print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
3583 795b88cb 2023-07-10 thomas {
3584 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3585 795b88cb 2023-07-10 thomas
3586 795b88cb 2023-07-10 thomas if (header != NULL)
3587 795b88cb 2023-07-10 thomas printf("%s\n", header);
3588 795b88cb 2023-07-10 thomas
3589 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
3590 795b88cb 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
3591 795b88cb 2023-07-10 thomas int pad = dsa->max_path_len - pe->path_len + 1;
3592 795b88cb 2023-07-10 thomas
3593 795b88cb 2023-07-10 thomas printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
3594 795b88cb 2023-07-10 thomas ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
3595 795b88cb 2023-07-10 thomas }
3596 795b88cb 2023-07-10 thomas printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
3597 795b88cb 2023-07-10 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
3598 795b88cb 2023-07-10 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
3599 795b88cb 2023-07-10 thomas
3600 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0)
3601 795b88cb 2023-07-10 thomas return got_error_from_errno("fflush");
3602 795b88cb 2023-07-10 thomas
3603 795b88cb 2023-07-10 thomas return NULL;
3604 795b88cb 2023-07-10 thomas }
3605 795b88cb 2023-07-10 thomas
3606 795b88cb 2023-07-10 thomas static const struct got_error *
3607 795b88cb 2023-07-10 thomas printfile(FILE *f)
3608 795b88cb 2023-07-10 thomas {
3609 795b88cb 2023-07-10 thomas char buf[8192];
3610 795b88cb 2023-07-10 thomas size_t r;
3611 795b88cb 2023-07-10 thomas
3612 795b88cb 2023-07-10 thomas if (fseeko(f, 0L, SEEK_SET) == -1)
3613 795b88cb 2023-07-10 thomas return got_error_from_errno("fseek");
3614 795b88cb 2023-07-10 thomas
3615 795b88cb 2023-07-10 thomas for (;;) {
3616 795b88cb 2023-07-10 thomas r = fread(buf, 1, sizeof(buf), f);
3617 795b88cb 2023-07-10 thomas if (r == 0) {
3618 795b88cb 2023-07-10 thomas if (ferror(f))
3619 795b88cb 2023-07-10 thomas return got_error_from_errno("fread");
3620 795b88cb 2023-07-10 thomas if (feof(f))
3621 795b88cb 2023-07-10 thomas break;
3622 795b88cb 2023-07-10 thomas }
3623 795b88cb 2023-07-10 thomas if (fwrite(buf, 1, r, stdout) != r)
3624 795b88cb 2023-07-10 thomas return got_ferror(stdout, GOT_ERR_IO);
3625 795b88cb 2023-07-10 thomas }
3626 795b88cb 2023-07-10 thomas
3627 795b88cb 2023-07-10 thomas return NULL;
3628 795b88cb 2023-07-10 thomas }
3629 795b88cb 2023-07-10 thomas
3630 795b88cb 2023-07-10 thomas static const struct got_error *
3631 795b88cb 2023-07-10 thomas print_commit(struct got_commit_object *commit, struct got_object_id *id,
3632 795b88cb 2023-07-10 thomas struct got_repository *repo, const char *path,
3633 795b88cb 2023-07-10 thomas struct got_pathlist_head *changed_paths,
3634 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
3635 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
3636 795b88cb 2023-07-10 thomas const char *prefix)
3637 795b88cb 2023-07-10 thomas {
3638 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
3639 795b88cb 2023-07-10 thomas FILE *f = NULL;
3640 795b88cb 2023-07-10 thomas char *id_str, *datestr, *logmsg0, *logmsg, *line;
3641 795b88cb 2023-07-10 thomas char datebuf[26];
3642 795b88cb 2023-07-10 thomas time_t committer_time;
3643 795b88cb 2023-07-10 thomas const char *author, *committer;
3644 795b88cb 2023-07-10 thomas char *refs_str = NULL;
3645 795b88cb 2023-07-10 thomas
3646 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
3647 795b88cb 2023-07-10 thomas if (err)
3648 795b88cb 2023-07-10 thomas return err;
3649 795b88cb 2023-07-10 thomas
3650 795b88cb 2023-07-10 thomas if (custom_refs_str == NULL) {
3651 795b88cb 2023-07-10 thomas struct got_reflist_head *refs;
3652 795b88cb 2023-07-10 thomas refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3653 795b88cb 2023-07-10 thomas if (refs) {
3654 795b88cb 2023-07-10 thomas err = build_refs_str(&refs_str, refs, id, repo, 0);
3655 795b88cb 2023-07-10 thomas if (err)
3656 795b88cb 2023-07-10 thomas goto done;
3657 795b88cb 2023-07-10 thomas }
3658 795b88cb 2023-07-10 thomas }
3659 795b88cb 2023-07-10 thomas
3660 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
3661 795b88cb 2023-07-10 thomas if (custom_refs_str)
3662 795b88cb 2023-07-10 thomas printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
3663 795b88cb 2023-07-10 thomas custom_refs_str);
3664 795b88cb 2023-07-10 thomas else
3665 795b88cb 2023-07-10 thomas printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
3666 795b88cb 2023-07-10 thomas refs_str ? " (" : "", refs_str ? refs_str : "",
3667 795b88cb 2023-07-10 thomas refs_str ? ")" : "");
3668 795b88cb 2023-07-10 thomas free(id_str);
3669 795b88cb 2023-07-10 thomas id_str = NULL;
3670 795b88cb 2023-07-10 thomas free(refs_str);
3671 795b88cb 2023-07-10 thomas refs_str = NULL;
3672 795b88cb 2023-07-10 thomas printf("from: %s\n", got_object_commit_get_author(commit));
3673 795b88cb 2023-07-10 thomas author = got_object_commit_get_author(commit);
3674 795b88cb 2023-07-10 thomas committer = got_object_commit_get_committer(commit);
3675 795b88cb 2023-07-10 thomas if (strcmp(author, committer) != 0)
3676 795b88cb 2023-07-10 thomas printf("via: %s\n", committer);
3677 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
3678 795b88cb 2023-07-10 thomas datestr = get_datestr(&committer_time, datebuf);
3679 795b88cb 2023-07-10 thomas if (datestr)
3680 795b88cb 2023-07-10 thomas printf("date: %s UTC\n", datestr);
3681 795b88cb 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 1) {
3682 795b88cb 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
3683 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3684 795b88cb 2023-07-10 thomas int n = 1;
3685 795b88cb 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3686 795b88cb 2023-07-10 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3687 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, &qid->id);
3688 795b88cb 2023-07-10 thomas if (err)
3689 795b88cb 2023-07-10 thomas goto done;
3690 795b88cb 2023-07-10 thomas printf("parent %d: %s\n", n++, id_str);
3691 795b88cb 2023-07-10 thomas free(id_str);
3692 795b88cb 2023-07-10 thomas id_str = NULL;
3693 795b88cb 2023-07-10 thomas }
3694 795b88cb 2023-07-10 thomas }
3695 795b88cb 2023-07-10 thomas
3696 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg0, commit);
3697 795b88cb 2023-07-10 thomas if (err)
3698 795b88cb 2023-07-10 thomas goto done;
3699 795b88cb 2023-07-10 thomas
3700 795b88cb 2023-07-10 thomas logmsg = logmsg0;
3701 795b88cb 2023-07-10 thomas do {
3702 795b88cb 2023-07-10 thomas line = strsep(&logmsg, "\n");
3703 795b88cb 2023-07-10 thomas if (line)
3704 795b88cb 2023-07-10 thomas printf(" %s\n", line);
3705 795b88cb 2023-07-10 thomas } while (line);
3706 795b88cb 2023-07-10 thomas free(logmsg0);
3707 795b88cb 2023-07-10 thomas
3708 795b88cb 2023-07-10 thomas if (changed_paths && diffstat == NULL) {
3709 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
3710 795b88cb 2023-07-10 thomas
3711 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, changed_paths, entry) {
3712 795b88cb 2023-07-10 thomas struct got_diff_changed_path *cp = pe->data;
3713 795b88cb 2023-07-10 thomas
3714 795b88cb 2023-07-10 thomas printf(" %c %s\n", cp->status, pe->path);
3715 795b88cb 2023-07-10 thomas }
3716 795b88cb 2023-07-10 thomas printf("\n");
3717 795b88cb 2023-07-10 thomas }
3718 795b88cb 2023-07-10 thomas if (show_patch) {
3719 795b88cb 2023-07-10 thomas if (diffstat) {
3720 795b88cb 2023-07-10 thomas f = got_opentemp();
3721 795b88cb 2023-07-10 thomas if (f == NULL) {
3722 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentemp");
3723 795b88cb 2023-07-10 thomas goto done;
3724 795b88cb 2023-07-10 thomas }
3725 795b88cb 2023-07-10 thomas }
3726 795b88cb 2023-07-10 thomas
3727 795b88cb 2023-07-10 thomas err = print_patch(commit, id, path, diff_context, diffstat,
3728 795b88cb 2023-07-10 thomas repo, diffstat == NULL ? stdout : f);
3729 795b88cb 2023-07-10 thomas if (err)
3730 795b88cb 2023-07-10 thomas goto done;
3731 795b88cb 2023-07-10 thomas }
3732 795b88cb 2023-07-10 thomas if (diffstat) {
3733 795b88cb 2023-07-10 thomas err = print_diffstat(diffstat, NULL);
3734 795b88cb 2023-07-10 thomas if (err)
3735 795b88cb 2023-07-10 thomas goto done;
3736 795b88cb 2023-07-10 thomas if (show_patch) {
3737 795b88cb 2023-07-10 thomas err = printfile(f);
3738 795b88cb 2023-07-10 thomas if (err)
3739 795b88cb 2023-07-10 thomas goto done;
3740 795b88cb 2023-07-10 thomas }
3741 795b88cb 2023-07-10 thomas }
3742 795b88cb 2023-07-10 thomas if (show_patch)
3743 795b88cb 2023-07-10 thomas printf("\n");
3744 795b88cb 2023-07-10 thomas
3745 795b88cb 2023-07-10 thomas if (fflush(stdout) != 0 && err == NULL)
3746 795b88cb 2023-07-10 thomas err = got_error_from_errno("fflush");
3747 795b88cb 2023-07-10 thomas done:
3748 795b88cb 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
3749 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
3750 795b88cb 2023-07-10 thomas free(id_str);
3751 795b88cb 2023-07-10 thomas free(refs_str);
3752 795b88cb 2023-07-10 thomas return err;
3753 795b88cb 2023-07-10 thomas }
3754 795b88cb 2023-07-10 thomas
3755 795b88cb 2023-07-10 thomas static const struct got_error *
3756 795b88cb 2023-07-10 thomas print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3757 795b88cb 2023-07-10 thomas struct got_repository *repo, const char *path, int show_changed_paths,
3758 795b88cb 2023-07-10 thomas int show_diffstat, int show_patch, const char *search_pattern,
3759 795b88cb 2023-07-10 thomas int diff_context, int limit, int log_branches, int reverse_display_order,
3760 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap, int one_line,
3761 795b88cb 2023-07-10 thomas FILE *tmpfile)
3762 795b88cb 2023-07-10 thomas {
3763 795b88cb 2023-07-10 thomas const struct got_error *err;
3764 795b88cb 2023-07-10 thomas struct got_commit_graph *graph;
3765 795b88cb 2023-07-10 thomas regex_t regex;
3766 795b88cb 2023-07-10 thomas int have_match;
3767 795b88cb 2023-07-10 thomas struct got_object_id_queue reversed_commits;
3768 795b88cb 2023-07-10 thomas struct got_object_qid *qid;
3769 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
3770 795b88cb 2023-07-10 thomas struct got_pathlist_head changed_paths;
3771 795b88cb 2023-07-10 thomas
3772 795b88cb 2023-07-10 thomas STAILQ_INIT(&reversed_commits);
3773 795b88cb 2023-07-10 thomas TAILQ_INIT(&changed_paths);
3774 795b88cb 2023-07-10 thomas
3775 795b88cb 2023-07-10 thomas if (search_pattern && regcomp(&regex, search_pattern,
3776 795b88cb 2023-07-10 thomas REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3777 795b88cb 2023-07-10 thomas return got_error_msg(GOT_ERR_REGEX, search_pattern);
3778 795b88cb 2023-07-10 thomas
3779 795b88cb 2023-07-10 thomas err = got_commit_graph_open(&graph, path, !log_branches);
3780 795b88cb 2023-07-10 thomas if (err)
3781 795b88cb 2023-07-10 thomas return err;
3782 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_start(graph, root_id, repo,
3783 795b88cb 2023-07-10 thomas check_cancelled, NULL);
3784 795b88cb 2023-07-10 thomas if (err)
3785 795b88cb 2023-07-10 thomas goto done;
3786 795b88cb 2023-07-10 thomas for (;;) {
3787 795b88cb 2023-07-10 thomas struct got_object_id id;
3788 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3789 795b88cb 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3790 795b88cb 2023-07-10 thomas
3791 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
3792 795b88cb 2023-07-10 thomas break;
3793 795b88cb 2023-07-10 thomas
3794 795b88cb 2023-07-10 thomas err = got_commit_graph_iter_next(&id, graph, repo,
3795 795b88cb 2023-07-10 thomas check_cancelled, NULL);
3796 795b88cb 2023-07-10 thomas if (err) {
3797 795b88cb 2023-07-10 thomas if (err->code == GOT_ERR_ITER_COMPLETED)
3798 795b88cb 2023-07-10 thomas err = NULL;
3799 795b88cb 2023-07-10 thomas break;
3800 795b88cb 2023-07-10 thomas }
3801 795b88cb 2023-07-10 thomas
3802 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, &id);
3803 795b88cb 2023-07-10 thomas if (err)
3804 795b88cb 2023-07-10 thomas break;
3805 795b88cb 2023-07-10 thomas
3806 795b88cb 2023-07-10 thomas if ((show_changed_paths || (show_diffstat && !show_patch))
3807 795b88cb 2023-07-10 thomas && !reverse_display_order) {
3808 795b88cb 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit, repo,
3809 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL);
3810 795b88cb 2023-07-10 thomas if (err)
3811 795b88cb 2023-07-10 thomas break;
3812 795b88cb 2023-07-10 thomas }
3813 795b88cb 2023-07-10 thomas
3814 795b88cb 2023-07-10 thomas if (search_pattern) {
3815 795b88cb 2023-07-10 thomas err = match_commit(&have_match, &id, commit, &regex);
3816 795b88cb 2023-07-10 thomas if (err) {
3817 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3818 795b88cb 2023-07-10 thomas break;
3819 795b88cb 2023-07-10 thomas }
3820 795b88cb 2023-07-10 thomas if (have_match == 0 && show_changed_paths)
3821 795b88cb 2023-07-10 thomas match_changed_paths(&have_match,
3822 795b88cb 2023-07-10 thomas &changed_paths, &regex);
3823 795b88cb 2023-07-10 thomas if (have_match == 0 && show_patch) {
3824 795b88cb 2023-07-10 thomas err = match_patch(&have_match, commit, &id,
3825 795b88cb 2023-07-10 thomas path, diff_context, repo, &regex, tmpfile);
3826 795b88cb 2023-07-10 thomas if (err)
3827 795b88cb 2023-07-10 thomas break;
3828 795b88cb 2023-07-10 thomas }
3829 795b88cb 2023-07-10 thomas if (have_match == 0) {
3830 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3831 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths,
3832 795b88cb 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
3833 795b88cb 2023-07-10 thomas continue;
3834 795b88cb 2023-07-10 thomas }
3835 795b88cb 2023-07-10 thomas }
3836 795b88cb 2023-07-10 thomas
3837 795b88cb 2023-07-10 thomas if (reverse_display_order) {
3838 795b88cb 2023-07-10 thomas err = got_object_qid_alloc(&qid, &id);
3839 795b88cb 2023-07-10 thomas if (err)
3840 795b88cb 2023-07-10 thomas break;
3841 795b88cb 2023-07-10 thomas STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3842 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3843 795b88cb 2023-07-10 thomas } else {
3844 795b88cb 2023-07-10 thomas if (one_line)
3845 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, &id,
3846 795b88cb 2023-07-10 thomas repo, refs_idmap);
3847 795b88cb 2023-07-10 thomas else
3848 795b88cb 2023-07-10 thomas err = print_commit(commit, &id, repo, path,
3849 795b88cb 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
3850 795b88cb 2023-07-10 thomas &changed_paths : NULL,
3851 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
3852 795b88cb 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
3853 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3854 795b88cb 2023-07-10 thomas if (err)
3855 795b88cb 2023-07-10 thomas break;
3856 795b88cb 2023-07-10 thomas }
3857 795b88cb 2023-07-10 thomas if ((limit && --limit == 0) ||
3858 795b88cb 2023-07-10 thomas (end_id && got_object_id_cmp(&id, end_id) == 0))
3859 795b88cb 2023-07-10 thomas break;
3860 795b88cb 2023-07-10 thomas
3861 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3862 795b88cb 2023-07-10 thomas }
3863 795b88cb 2023-07-10 thomas if (reverse_display_order) {
3864 795b88cb 2023-07-10 thomas STAILQ_FOREACH(qid, &reversed_commits, entry) {
3865 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
3866 795b88cb 2023-07-10 thomas &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
3867 795b88cb 2023-07-10 thomas
3868 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo,
3869 795b88cb 2023-07-10 thomas &qid->id);
3870 795b88cb 2023-07-10 thomas if (err)
3871 795b88cb 2023-07-10 thomas break;
3872 795b88cb 2023-07-10 thomas if (show_changed_paths ||
3873 795b88cb 2023-07-10 thomas (show_diffstat && !show_patch)) {
3874 795b88cb 2023-07-10 thomas err = get_changed_paths(&changed_paths, commit,
3875 795b88cb 2023-07-10 thomas repo, show_diffstat ? &dsa : NULL);
3876 795b88cb 2023-07-10 thomas if (err)
3877 795b88cb 2023-07-10 thomas break;
3878 795b88cb 2023-07-10 thomas }
3879 795b88cb 2023-07-10 thomas if (one_line)
3880 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, &qid->id,
3881 795b88cb 2023-07-10 thomas repo, refs_idmap);
3882 795b88cb 2023-07-10 thomas else
3883 795b88cb 2023-07-10 thomas err = print_commit(commit, &qid->id, repo, path,
3884 795b88cb 2023-07-10 thomas (show_changed_paths || show_diffstat) ?
3885 795b88cb 2023-07-10 thomas &changed_paths : NULL,
3886 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, show_patch,
3887 795b88cb 2023-07-10 thomas diff_context, refs_idmap, NULL, NULL);
3888 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
3889 795b88cb 2023-07-10 thomas if (err)
3890 795b88cb 2023-07-10 thomas break;
3891 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3892 795b88cb 2023-07-10 thomas }
3893 795b88cb 2023-07-10 thomas }
3894 795b88cb 2023-07-10 thomas done:
3895 795b88cb 2023-07-10 thomas while (!STAILQ_EMPTY(&reversed_commits)) {
3896 795b88cb 2023-07-10 thomas qid = STAILQ_FIRST(&reversed_commits);
3897 795b88cb 2023-07-10 thomas STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3898 795b88cb 2023-07-10 thomas got_object_qid_free(qid);
3899 795b88cb 2023-07-10 thomas }
3900 795b88cb 2023-07-10 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
3901 795b88cb 2023-07-10 thomas if (search_pattern)
3902 795b88cb 2023-07-10 thomas regfree(&regex);
3903 795b88cb 2023-07-10 thomas got_commit_graph_close(graph);
3904 795b88cb 2023-07-10 thomas return err;
3905 795b88cb 2023-07-10 thomas }
3906 795b88cb 2023-07-10 thomas
3907 795b88cb 2023-07-10 thomas __dead static void
3908 795b88cb 2023-07-10 thomas usage_log(void)
3909 795b88cb 2023-07-10 thomas {
3910 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
3911 795b88cb 2023-07-10 thomas "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
3912 795b88cb 2023-07-10 thomas "[path]\n", getprogname());
3913 795b88cb 2023-07-10 thomas exit(1);
3914 795b88cb 2023-07-10 thomas }
3915 795b88cb 2023-07-10 thomas
3916 795b88cb 2023-07-10 thomas static int
3917 795b88cb 2023-07-10 thomas get_default_log_limit(void)
3918 795b88cb 2023-07-10 thomas {
3919 795b88cb 2023-07-10 thomas const char *got_default_log_limit;
3920 795b88cb 2023-07-10 thomas long long n;
3921 795b88cb 2023-07-10 thomas const char *errstr;
3922 795b88cb 2023-07-10 thomas
3923 795b88cb 2023-07-10 thomas got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3924 795b88cb 2023-07-10 thomas if (got_default_log_limit == NULL)
3925 795b88cb 2023-07-10 thomas return 0;
3926 795b88cb 2023-07-10 thomas n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3927 795b88cb 2023-07-10 thomas if (errstr != NULL)
3928 795b88cb 2023-07-10 thomas return 0;
3929 795b88cb 2023-07-10 thomas return n;
3930 795b88cb 2023-07-10 thomas }
3931 795b88cb 2023-07-10 thomas
3932 795b88cb 2023-07-10 thomas static const struct got_error *
3933 795b88cb 2023-07-10 thomas cmd_log(int argc, char *argv[])
3934 795b88cb 2023-07-10 thomas {
3935 795b88cb 2023-07-10 thomas const struct got_error *error;
3936 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
3937 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
3938 795b88cb 2023-07-10 thomas struct got_object_id *start_id = NULL, *end_id = NULL;
3939 795b88cb 2023-07-10 thomas char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3940 795b88cb 2023-07-10 thomas const char *start_commit = NULL, *end_commit = NULL;
3941 795b88cb 2023-07-10 thomas const char *search_pattern = NULL;
3942 795b88cb 2023-07-10 thomas int diff_context = -1, ch;
3943 795b88cb 2023-07-10 thomas int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3944 795b88cb 2023-07-10 thomas int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
3945 795b88cb 2023-07-10 thomas const char *errstr;
3946 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
3947 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
3948 795b88cb 2023-07-10 thomas FILE *tmpfile = NULL;
3949 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
3950 795b88cb 2023-07-10 thomas
3951 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
3952 795b88cb 2023-07-10 thomas
3953 795b88cb 2023-07-10 thomas #ifndef PROFILE
3954 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3955 795b88cb 2023-07-10 thomas NULL)
3956 795b88cb 2023-07-10 thomas == -1)
3957 795b88cb 2023-07-10 thomas err(1, "pledge");
3958 795b88cb 2023-07-10 thomas #endif
3959 795b88cb 2023-07-10 thomas
3960 795b88cb 2023-07-10 thomas limit = get_default_log_limit();
3961 795b88cb 2023-07-10 thomas
3962 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
3963 795b88cb 2023-07-10 thomas switch (ch) {
3964 795b88cb 2023-07-10 thomas case 'b':
3965 795b88cb 2023-07-10 thomas log_branches = 1;
3966 795b88cb 2023-07-10 thomas break;
3967 795b88cb 2023-07-10 thomas case 'C':
3968 795b88cb 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3969 795b88cb 2023-07-10 thomas &errstr);
3970 795b88cb 2023-07-10 thomas if (errstr != NULL)
3971 795b88cb 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
3972 795b88cb 2023-07-10 thomas errstr, optarg);
3973 795b88cb 2023-07-10 thomas break;
3974 795b88cb 2023-07-10 thomas case 'c':
3975 795b88cb 2023-07-10 thomas start_commit = optarg;
3976 795b88cb 2023-07-10 thomas break;
3977 795b88cb 2023-07-10 thomas case 'd':
3978 795b88cb 2023-07-10 thomas show_diffstat = 1;
3979 795b88cb 2023-07-10 thomas break;
3980 795b88cb 2023-07-10 thomas case 'l':
3981 795b88cb 2023-07-10 thomas limit = strtonum(optarg, 0, INT_MAX, &errstr);
3982 795b88cb 2023-07-10 thomas if (errstr != NULL)
3983 795b88cb 2023-07-10 thomas errx(1, "number of commits is %s: %s",
3984 795b88cb 2023-07-10 thomas errstr, optarg);
3985 795b88cb 2023-07-10 thomas break;
3986 795b88cb 2023-07-10 thomas case 'P':
3987 795b88cb 2023-07-10 thomas show_changed_paths = 1;
3988 795b88cb 2023-07-10 thomas break;
3989 795b88cb 2023-07-10 thomas case 'p':
3990 795b88cb 2023-07-10 thomas show_patch = 1;
3991 795b88cb 2023-07-10 thomas break;
3992 795b88cb 2023-07-10 thomas case 'R':
3993 795b88cb 2023-07-10 thomas reverse_display_order = 1;
3994 795b88cb 2023-07-10 thomas break;
3995 795b88cb 2023-07-10 thomas case 'r':
3996 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
3997 795b88cb 2023-07-10 thomas if (repo_path == NULL)
3998 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
3999 795b88cb 2023-07-10 thomas optarg);
4000 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4001 795b88cb 2023-07-10 thomas break;
4002 795b88cb 2023-07-10 thomas case 'S':
4003 795b88cb 2023-07-10 thomas search_pattern = optarg;
4004 795b88cb 2023-07-10 thomas break;
4005 795b88cb 2023-07-10 thomas case 's':
4006 795b88cb 2023-07-10 thomas one_line = 1;
4007 795b88cb 2023-07-10 thomas break;
4008 795b88cb 2023-07-10 thomas case 'x':
4009 795b88cb 2023-07-10 thomas end_commit = optarg;
4010 795b88cb 2023-07-10 thomas break;
4011 795b88cb 2023-07-10 thomas default:
4012 795b88cb 2023-07-10 thomas usage_log();
4013 795b88cb 2023-07-10 thomas /* NOTREACHED */
4014 795b88cb 2023-07-10 thomas }
4015 795b88cb 2023-07-10 thomas }
4016 795b88cb 2023-07-10 thomas
4017 795b88cb 2023-07-10 thomas argc -= optind;
4018 795b88cb 2023-07-10 thomas argv += optind;
4019 795b88cb 2023-07-10 thomas
4020 795b88cb 2023-07-10 thomas if (diff_context == -1)
4021 795b88cb 2023-07-10 thomas diff_context = 3;
4022 795b88cb 2023-07-10 thomas else if (!show_patch)
4023 795b88cb 2023-07-10 thomas errx(1, "-C requires -p");
4024 795b88cb 2023-07-10 thomas
4025 795b88cb 2023-07-10 thomas if (one_line && (show_patch || show_changed_paths || show_diffstat))
4026 795b88cb 2023-07-10 thomas errx(1, "cannot use -s with -d, -p or -P");
4027 795b88cb 2023-07-10 thomas
4028 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
4029 795b88cb 2023-07-10 thomas if (cwd == NULL) {
4030 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
4031 795b88cb 2023-07-10 thomas goto done;
4032 795b88cb 2023-07-10 thomas }
4033 795b88cb 2023-07-10 thomas
4034 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4035 795b88cb 2023-07-10 thomas if (error != NULL)
4036 795b88cb 2023-07-10 thomas goto done;
4037 795b88cb 2023-07-10 thomas
4038 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4039 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
4040 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4041 795b88cb 2023-07-10 thomas goto done;
4042 795b88cb 2023-07-10 thomas error = NULL;
4043 795b88cb 2023-07-10 thomas }
4044 795b88cb 2023-07-10 thomas
4045 795b88cb 2023-07-10 thomas if (argc == 1) {
4046 795b88cb 2023-07-10 thomas if (worktree) {
4047 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&path, worktree,
4048 795b88cb 2023-07-10 thomas argv[0]);
4049 795b88cb 2023-07-10 thomas if (error)
4050 795b88cb 2023-07-10 thomas goto done;
4051 795b88cb 2023-07-10 thomas } else {
4052 795b88cb 2023-07-10 thomas path = strdup(argv[0]);
4053 795b88cb 2023-07-10 thomas if (path == NULL) {
4054 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4055 795b88cb 2023-07-10 thomas goto done;
4056 795b88cb 2023-07-10 thomas }
4057 795b88cb 2023-07-10 thomas }
4058 795b88cb 2023-07-10 thomas } else if (argc != 0)
4059 795b88cb 2023-07-10 thomas usage_log();
4060 795b88cb 2023-07-10 thomas
4061 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4062 795b88cb 2023-07-10 thomas repo_path = worktree ?
4063 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4064 795b88cb 2023-07-10 thomas }
4065 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4066 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4067 795b88cb 2023-07-10 thomas goto done;
4068 795b88cb 2023-07-10 thomas }
4069 795b88cb 2023-07-10 thomas
4070 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4071 795b88cb 2023-07-10 thomas if (error != NULL)
4072 795b88cb 2023-07-10 thomas goto done;
4073 795b88cb 2023-07-10 thomas
4074 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4075 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4076 795b88cb 2023-07-10 thomas if (error)
4077 795b88cb 2023-07-10 thomas goto done;
4078 795b88cb 2023-07-10 thomas
4079 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, 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_reflist_object_id_map_create(&refs_idmap, &refs, repo);
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 if (start_commit == NULL) {
4088 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
4089 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
4090 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
4091 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
4092 795b88cb 2023-07-10 thomas : GOT_REF_HEAD, 0);
4093 795b88cb 2023-07-10 thomas if (error != NULL)
4094 795b88cb 2023-07-10 thomas goto done;
4095 795b88cb 2023-07-10 thomas error = got_ref_resolve(&start_id, repo, head_ref);
4096 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
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_object_open_as_commit(&commit, repo,
4100 795b88cb 2023-07-10 thomas start_id);
4101 795b88cb 2023-07-10 thomas if (error != NULL)
4102 795b88cb 2023-07-10 thomas goto done;
4103 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4104 795b88cb 2023-07-10 thomas } else {
4105 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&start_id, NULL,
4106 795b88cb 2023-07-10 thomas start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4107 795b88cb 2023-07-10 thomas if (error != NULL)
4108 795b88cb 2023-07-10 thomas goto done;
4109 795b88cb 2023-07-10 thomas }
4110 795b88cb 2023-07-10 thomas if (end_commit != NULL) {
4111 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&end_id, NULL,
4112 795b88cb 2023-07-10 thomas end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4113 795b88cb 2023-07-10 thomas if (error != NULL)
4114 795b88cb 2023-07-10 thomas goto done;
4115 795b88cb 2023-07-10 thomas }
4116 795b88cb 2023-07-10 thomas
4117 795b88cb 2023-07-10 thomas if (worktree) {
4118 795b88cb 2023-07-10 thomas /*
4119 795b88cb 2023-07-10 thomas * If a path was specified on the command line it was resolved
4120 795b88cb 2023-07-10 thomas * to a path in the work tree above. Prepend the work tree's
4121 795b88cb 2023-07-10 thomas * path prefix to obtain the corresponding in-repository path.
4122 795b88cb 2023-07-10 thomas */
4123 795b88cb 2023-07-10 thomas if (path) {
4124 795b88cb 2023-07-10 thomas const char *prefix;
4125 795b88cb 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4126 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4127 795b88cb 2023-07-10 thomas (path[0] != '\0') ? "/" : "", path) == -1) {
4128 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4129 795b88cb 2023-07-10 thomas goto done;
4130 795b88cb 2023-07-10 thomas }
4131 795b88cb 2023-07-10 thomas }
4132 795b88cb 2023-07-10 thomas } else
4133 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo,
4134 795b88cb 2023-07-10 thomas path ? path : "");
4135 795b88cb 2023-07-10 thomas if (error != NULL)
4136 795b88cb 2023-07-10 thomas goto done;
4137 795b88cb 2023-07-10 thomas if (in_repo_path) {
4138 795b88cb 2023-07-10 thomas free(path);
4139 795b88cb 2023-07-10 thomas path = in_repo_path;
4140 795b88cb 2023-07-10 thomas }
4141 795b88cb 2023-07-10 thomas
4142 795b88cb 2023-07-10 thomas if (worktree) {
4143 795b88cb 2023-07-10 thomas /* Release work tree lock. */
4144 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4145 795b88cb 2023-07-10 thomas worktree = NULL;
4146 795b88cb 2023-07-10 thomas }
4147 795b88cb 2023-07-10 thomas
4148 795b88cb 2023-07-10 thomas if (search_pattern && show_patch) {
4149 795b88cb 2023-07-10 thomas tmpfile = got_opentemp();
4150 795b88cb 2023-07-10 thomas if (tmpfile == NULL) {
4151 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4152 795b88cb 2023-07-10 thomas goto done;
4153 795b88cb 2023-07-10 thomas }
4154 795b88cb 2023-07-10 thomas }
4155 795b88cb 2023-07-10 thomas
4156 795b88cb 2023-07-10 thomas error = print_commits(start_id, end_id, repo, path ? path : "",
4157 795b88cb 2023-07-10 thomas show_changed_paths, show_diffstat, show_patch, search_pattern,
4158 795b88cb 2023-07-10 thomas diff_context, limit, log_branches, reverse_display_order,
4159 795b88cb 2023-07-10 thomas refs_idmap, one_line, tmpfile);
4160 795b88cb 2023-07-10 thomas done:
4161 795b88cb 2023-07-10 thomas free(path);
4162 795b88cb 2023-07-10 thomas free(repo_path);
4163 795b88cb 2023-07-10 thomas free(cwd);
4164 609a7400 2023-07-17 thomas free(start_id);
4165 609a7400 2023-07-17 thomas free(end_id);
4166 795b88cb 2023-07-10 thomas if (worktree)
4167 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4168 795b88cb 2023-07-10 thomas if (repo) {
4169 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4170 795b88cb 2023-07-10 thomas if (error == NULL)
4171 795b88cb 2023-07-10 thomas error = close_err;
4172 795b88cb 2023-07-10 thomas }
4173 795b88cb 2023-07-10 thomas if (pack_fds) {
4174 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
4175 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4176 795b88cb 2023-07-10 thomas if (error == NULL)
4177 795b88cb 2023-07-10 thomas error = pack_err;
4178 795b88cb 2023-07-10 thomas }
4179 795b88cb 2023-07-10 thomas if (refs_idmap)
4180 795b88cb 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
4181 795b88cb 2023-07-10 thomas if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4182 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4183 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
4184 795b88cb 2023-07-10 thomas return error;
4185 795b88cb 2023-07-10 thomas }
4186 795b88cb 2023-07-10 thomas
4187 795b88cb 2023-07-10 thomas __dead static void
4188 795b88cb 2023-07-10 thomas usage_diff(void)
4189 795b88cb 2023-07-10 thomas {
4190 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4191 795b88cb 2023-07-10 thomas "[-r repository-path] [object1 object2 | path ...]\n",
4192 795b88cb 2023-07-10 thomas getprogname());
4193 795b88cb 2023-07-10 thomas exit(1);
4194 795b88cb 2023-07-10 thomas }
4195 795b88cb 2023-07-10 thomas
4196 795b88cb 2023-07-10 thomas struct print_diff_arg {
4197 795b88cb 2023-07-10 thomas struct got_repository *repo;
4198 795b88cb 2023-07-10 thomas struct got_worktree *worktree;
4199 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg *diffstat;
4200 795b88cb 2023-07-10 thomas int diff_context;
4201 795b88cb 2023-07-10 thomas const char *id_str;
4202 795b88cb 2023-07-10 thomas int header_shown;
4203 795b88cb 2023-07-10 thomas int diff_staged;
4204 795b88cb 2023-07-10 thomas enum got_diff_algorithm diff_algo;
4205 795b88cb 2023-07-10 thomas int ignore_whitespace;
4206 795b88cb 2023-07-10 thomas int force_text_diff;
4207 795b88cb 2023-07-10 thomas FILE *f1;
4208 795b88cb 2023-07-10 thomas FILE *f2;
4209 795b88cb 2023-07-10 thomas FILE *outfile;
4210 795b88cb 2023-07-10 thomas };
4211 795b88cb 2023-07-10 thomas
4212 795b88cb 2023-07-10 thomas /*
4213 795b88cb 2023-07-10 thomas * Create a file which contains the target path of a symlink so we can feed
4214 795b88cb 2023-07-10 thomas * it as content to the diff engine.
4215 795b88cb 2023-07-10 thomas */
4216 795b88cb 2023-07-10 thomas static const struct got_error *
4217 795b88cb 2023-07-10 thomas get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4218 795b88cb 2023-07-10 thomas const char *abspath)
4219 795b88cb 2023-07-10 thomas {
4220 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4221 795b88cb 2023-07-10 thomas char target_path[PATH_MAX];
4222 795b88cb 2023-07-10 thomas ssize_t target_len, outlen;
4223 795b88cb 2023-07-10 thomas
4224 795b88cb 2023-07-10 thomas *fd = -1;
4225 795b88cb 2023-07-10 thomas
4226 795b88cb 2023-07-10 thomas if (dirfd != -1) {
4227 795b88cb 2023-07-10 thomas target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4228 795b88cb 2023-07-10 thomas if (target_len == -1)
4229 795b88cb 2023-07-10 thomas return got_error_from_errno2("readlinkat", abspath);
4230 795b88cb 2023-07-10 thomas } else {
4231 795b88cb 2023-07-10 thomas target_len = readlink(abspath, 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("readlink", abspath);
4234 795b88cb 2023-07-10 thomas }
4235 795b88cb 2023-07-10 thomas
4236 795b88cb 2023-07-10 thomas *fd = got_opentempfd();
4237 795b88cb 2023-07-10 thomas if (*fd == -1)
4238 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
4239 795b88cb 2023-07-10 thomas
4240 795b88cb 2023-07-10 thomas outlen = write(*fd, target_path, target_len);
4241 795b88cb 2023-07-10 thomas if (outlen == -1) {
4242 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4243 795b88cb 2023-07-10 thomas goto done;
4244 795b88cb 2023-07-10 thomas }
4245 795b88cb 2023-07-10 thomas
4246 795b88cb 2023-07-10 thomas if (lseek(*fd, 0, SEEK_SET) == -1) {
4247 795b88cb 2023-07-10 thomas err = got_error_from_errno2("lseek", abspath);
4248 795b88cb 2023-07-10 thomas goto done;
4249 795b88cb 2023-07-10 thomas }
4250 795b88cb 2023-07-10 thomas done:
4251 795b88cb 2023-07-10 thomas if (err) {
4252 795b88cb 2023-07-10 thomas close(*fd);
4253 795b88cb 2023-07-10 thomas *fd = -1;
4254 795b88cb 2023-07-10 thomas }
4255 795b88cb 2023-07-10 thomas return err;
4256 795b88cb 2023-07-10 thomas }
4257 795b88cb 2023-07-10 thomas
4258 795b88cb 2023-07-10 thomas static const struct got_error *
4259 795b88cb 2023-07-10 thomas print_diff(void *arg, unsigned char status, unsigned char staged_status,
4260 795b88cb 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
4261 795b88cb 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4262 795b88cb 2023-07-10 thomas int dirfd, const char *de_name)
4263 795b88cb 2023-07-10 thomas {
4264 795b88cb 2023-07-10 thomas struct print_diff_arg *a = arg;
4265 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4266 795b88cb 2023-07-10 thomas struct got_blob_object *blob1 = NULL;
4267 795b88cb 2023-07-10 thomas int fd = -1, fd1 = -1, fd2 = -1;
4268 795b88cb 2023-07-10 thomas FILE *f2 = NULL;
4269 795b88cb 2023-07-10 thomas char *abspath = NULL, *label1 = NULL;
4270 795b88cb 2023-07-10 thomas struct stat sb;
4271 795b88cb 2023-07-10 thomas off_t size1 = 0;
4272 795b88cb 2023-07-10 thomas int f2_exists = 0;
4273 795b88cb 2023-07-10 thomas
4274 795b88cb 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
4275 795b88cb 2023-07-10 thomas
4276 795b88cb 2023-07-10 thomas if (a->diff_staged) {
4277 795b88cb 2023-07-10 thomas if (staged_status != GOT_STATUS_MODIFY &&
4278 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_ADD &&
4279 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_DELETE)
4280 795b88cb 2023-07-10 thomas return NULL;
4281 795b88cb 2023-07-10 thomas } else {
4282 795b88cb 2023-07-10 thomas if (staged_status == GOT_STATUS_DELETE)
4283 795b88cb 2023-07-10 thomas return NULL;
4284 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
4285 795b88cb 2023-07-10 thomas return got_error_set_errno(ENOENT, path);
4286 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_MODIFY &&
4287 795b88cb 2023-07-10 thomas status != GOT_STATUS_ADD &&
4288 795b88cb 2023-07-10 thomas status != GOT_STATUS_DELETE &&
4289 795b88cb 2023-07-10 thomas status != GOT_STATUS_CONFLICT)
4290 795b88cb 2023-07-10 thomas return NULL;
4291 795b88cb 2023-07-10 thomas }
4292 795b88cb 2023-07-10 thomas
4293 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(a->f1);
4294 795b88cb 2023-07-10 thomas if (err)
4295 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4296 795b88cb 2023-07-10 thomas err = got_opentemp_truncate(a->f2);
4297 795b88cb 2023-07-10 thomas if (err)
4298 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentemp_truncate");
4299 795b88cb 2023-07-10 thomas
4300 795b88cb 2023-07-10 thomas if (!a->header_shown) {
4301 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "diff %s%s\n",
4302 795b88cb 2023-07-10 thomas a->diff_staged ? "-s " : "",
4303 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree)) < 0) {
4304 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4305 795b88cb 2023-07-10 thomas goto done;
4306 795b88cb 2023-07-10 thomas }
4307 795b88cb 2023-07-10 thomas if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 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, "path + %s%s\n",
4312 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree),
4313 795b88cb 2023-07-10 thomas a->diff_staged ? " (staged changes)" : "") < 0) {
4314 795b88cb 2023-07-10 thomas err = got_error_from_errno("fprintf");
4315 795b88cb 2023-07-10 thomas goto done;
4316 795b88cb 2023-07-10 thomas }
4317 795b88cb 2023-07-10 thomas a->header_shown = 1;
4318 795b88cb 2023-07-10 thomas }
4319 795b88cb 2023-07-10 thomas
4320 795b88cb 2023-07-10 thomas if (a->diff_staged) {
4321 795b88cb 2023-07-10 thomas const char *label1 = NULL, *label2 = NULL;
4322 795b88cb 2023-07-10 thomas switch (staged_status) {
4323 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
4324 795b88cb 2023-07-10 thomas label1 = path;
4325 795b88cb 2023-07-10 thomas label2 = path;
4326 795b88cb 2023-07-10 thomas break;
4327 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
4328 795b88cb 2023-07-10 thomas label2 = path;
4329 795b88cb 2023-07-10 thomas break;
4330 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
4331 795b88cb 2023-07-10 thomas label1 = path;
4332 795b88cb 2023-07-10 thomas break;
4333 795b88cb 2023-07-10 thomas default:
4334 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_FILE_STATUS);
4335 795b88cb 2023-07-10 thomas }
4336 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4337 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4338 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4339 795b88cb 2023-07-10 thomas goto done;
4340 795b88cb 2023-07-10 thomas }
4341 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
4342 795b88cb 2023-07-10 thomas if (fd2 == -1) {
4343 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4344 795b88cb 2023-07-10 thomas goto done;
4345 795b88cb 2023-07-10 thomas }
4346 795b88cb 2023-07-10 thomas err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4347 795b88cb 2023-07-10 thomas fd1, fd2, blob_id, staged_blob_id, label1, label2,
4348 795b88cb 2023-07-10 thomas a->diff_algo, a->diff_context, a->ignore_whitespace,
4349 795b88cb 2023-07-10 thomas a->force_text_diff, a->diffstat, a->repo, a->outfile);
4350 795b88cb 2023-07-10 thomas goto done;
4351 795b88cb 2023-07-10 thomas }
4352 795b88cb 2023-07-10 thomas
4353 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4354 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4355 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_opentempfd");
4356 795b88cb 2023-07-10 thomas goto done;
4357 795b88cb 2023-07-10 thomas }
4358 795b88cb 2023-07-10 thomas
4359 795b88cb 2023-07-10 thomas if (staged_status == GOT_STATUS_ADD ||
4360 795b88cb 2023-07-10 thomas staged_status == GOT_STATUS_MODIFY) {
4361 795b88cb 2023-07-10 thomas char *id_str;
4362 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4363 795b88cb 2023-07-10 thomas 8192, fd1);
4364 795b88cb 2023-07-10 thomas if (err)
4365 795b88cb 2023-07-10 thomas goto done;
4366 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
4367 795b88cb 2023-07-10 thomas if (err)
4368 795b88cb 2023-07-10 thomas goto done;
4369 795b88cb 2023-07-10 thomas if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4370 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
4371 795b88cb 2023-07-10 thomas free(id_str);
4372 795b88cb 2023-07-10 thomas goto done;
4373 795b88cb 2023-07-10 thomas }
4374 795b88cb 2023-07-10 thomas free(id_str);
4375 795b88cb 2023-07-10 thomas } else if (status != GOT_STATUS_ADD) {
4376 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4377 795b88cb 2023-07-10 thomas fd1);
4378 795b88cb 2023-07-10 thomas if (err)
4379 795b88cb 2023-07-10 thomas goto done;
4380 795b88cb 2023-07-10 thomas }
4381 795b88cb 2023-07-10 thomas
4382 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_DELETE) {
4383 795b88cb 2023-07-10 thomas if (asprintf(&abspath, "%s/%s",
4384 795b88cb 2023-07-10 thomas got_worktree_get_root_path(a->worktree), path) == -1) {
4385 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
4386 795b88cb 2023-07-10 thomas goto done;
4387 795b88cb 2023-07-10 thomas }
4388 795b88cb 2023-07-10 thomas
4389 795b88cb 2023-07-10 thomas if (dirfd != -1) {
4390 795b88cb 2023-07-10 thomas fd = openat(dirfd, de_name,
4391 795b88cb 2023-07-10 thomas O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4392 795b88cb 2023-07-10 thomas if (fd == -1) {
4393 795b88cb 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
4394 795b88cb 2023-07-10 thomas err = got_error_from_errno2("openat",
4395 795b88cb 2023-07-10 thomas abspath);
4396 795b88cb 2023-07-10 thomas goto done;
4397 795b88cb 2023-07-10 thomas }
4398 795b88cb 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
4399 795b88cb 2023-07-10 thomas de_name, abspath);
4400 795b88cb 2023-07-10 thomas if (err)
4401 795b88cb 2023-07-10 thomas goto done;
4402 795b88cb 2023-07-10 thomas }
4403 795b88cb 2023-07-10 thomas } else {
4404 795b88cb 2023-07-10 thomas fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4405 795b88cb 2023-07-10 thomas if (fd == -1) {
4406 795b88cb 2023-07-10 thomas if (!got_err_open_nofollow_on_symlink()) {
4407 795b88cb 2023-07-10 thomas err = got_error_from_errno2("open",
4408 795b88cb 2023-07-10 thomas abspath);
4409 795b88cb 2023-07-10 thomas goto done;
4410 795b88cb 2023-07-10 thomas }
4411 795b88cb 2023-07-10 thomas err = get_symlink_target_file(&fd, dirfd,
4412 795b88cb 2023-07-10 thomas de_name, abspath);
4413 795b88cb 2023-07-10 thomas if (err)
4414 795b88cb 2023-07-10 thomas goto done;
4415 795b88cb 2023-07-10 thomas }
4416 795b88cb 2023-07-10 thomas }
4417 795b88cb 2023-07-10 thomas if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4418 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fstatat", abspath);
4419 795b88cb 2023-07-10 thomas goto done;
4420 795b88cb 2023-07-10 thomas }
4421 795b88cb 2023-07-10 thomas f2 = fdopen(fd, "r");
4422 795b88cb 2023-07-10 thomas if (f2 == NULL) {
4423 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fdopen", abspath);
4424 795b88cb 2023-07-10 thomas goto done;
4425 795b88cb 2023-07-10 thomas }
4426 795b88cb 2023-07-10 thomas fd = -1;
4427 795b88cb 2023-07-10 thomas f2_exists = 1;
4428 795b88cb 2023-07-10 thomas }
4429 795b88cb 2023-07-10 thomas
4430 795b88cb 2023-07-10 thomas if (blob1) {
4431 795b88cb 2023-07-10 thomas err = got_object_blob_dump_to_file(&size1, NULL, NULL,
4432 795b88cb 2023-07-10 thomas a->f1, blob1);
4433 795b88cb 2023-07-10 thomas if (err)
4434 795b88cb 2023-07-10 thomas goto done;
4435 795b88cb 2023-07-10 thomas }
4436 795b88cb 2023-07-10 thomas
4437 795b88cb 2023-07-10 thomas err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
4438 795b88cb 2023-07-10 thomas f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
4439 795b88cb 2023-07-10 thomas a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
4440 795b88cb 2023-07-10 thomas done:
4441 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4442 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4443 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4444 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4445 795b88cb 2023-07-10 thomas if (blob1)
4446 795b88cb 2023-07-10 thomas got_object_blob_close(blob1);
4447 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
4448 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
4449 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && err == NULL)
4450 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
4451 795b88cb 2023-07-10 thomas free(abspath);
4452 795b88cb 2023-07-10 thomas return err;
4453 795b88cb 2023-07-10 thomas }
4454 795b88cb 2023-07-10 thomas
4455 795b88cb 2023-07-10 thomas static const struct got_error *
4456 795b88cb 2023-07-10 thomas cmd_diff(int argc, char *argv[])
4457 795b88cb 2023-07-10 thomas {
4458 795b88cb 2023-07-10 thomas const struct got_error *error;
4459 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
4460 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
4461 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL;
4462 795b88cb 2023-07-10 thomas const char *commit_args[2] = { NULL, NULL };
4463 795b88cb 2023-07-10 thomas int ncommit_args = 0;
4464 795b88cb 2023-07-10 thomas struct got_object_id *ids[2] = { NULL, NULL };
4465 795b88cb 2023-07-10 thomas char *labels[2] = { NULL, NULL };
4466 795b88cb 2023-07-10 thomas int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4467 795b88cb 2023-07-10 thomas int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4468 795b88cb 2023-07-10 thomas int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
4469 795b88cb 2023-07-10 thomas const char *errstr;
4470 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
4471 795b88cb 2023-07-10 thomas struct got_pathlist_head diffstat_paths, paths;
4472 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4473 795b88cb 2023-07-10 thomas int fd1 = -1, fd2 = -1;
4474 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
4475 795b88cb 2023-07-10 thomas struct got_diffstat_cb_arg dsa;
4476 795b88cb 2023-07-10 thomas
4477 795b88cb 2023-07-10 thomas memset(&dsa, 0, sizeof(dsa));
4478 795b88cb 2023-07-10 thomas
4479 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
4480 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
4481 795b88cb 2023-07-10 thomas TAILQ_INIT(&diffstat_paths);
4482 795b88cb 2023-07-10 thomas
4483 795b88cb 2023-07-10 thomas #ifndef PROFILE
4484 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4485 795b88cb 2023-07-10 thomas NULL) == -1)
4486 795b88cb 2023-07-10 thomas err(1, "pledge");
4487 795b88cb 2023-07-10 thomas #endif
4488 795b88cb 2023-07-10 thomas
4489 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
4490 795b88cb 2023-07-10 thomas switch (ch) {
4491 795b88cb 2023-07-10 thomas case 'a':
4492 795b88cb 2023-07-10 thomas force_text_diff = 1;
4493 795b88cb 2023-07-10 thomas break;
4494 795b88cb 2023-07-10 thomas case 'C':
4495 795b88cb 2023-07-10 thomas diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4496 795b88cb 2023-07-10 thomas &errstr);
4497 795b88cb 2023-07-10 thomas if (errstr != NULL)
4498 795b88cb 2023-07-10 thomas errx(1, "number of context lines is %s: %s",
4499 795b88cb 2023-07-10 thomas errstr, optarg);
4500 795b88cb 2023-07-10 thomas break;
4501 795b88cb 2023-07-10 thomas case 'c':
4502 795b88cb 2023-07-10 thomas if (ncommit_args >= 2)
4503 795b88cb 2023-07-10 thomas errx(1, "too many -c options used");
4504 795b88cb 2023-07-10 thomas commit_args[ncommit_args++] = optarg;
4505 795b88cb 2023-07-10 thomas break;
4506 795b88cb 2023-07-10 thomas case 'd':
4507 795b88cb 2023-07-10 thomas show_diffstat = 1;
4508 795b88cb 2023-07-10 thomas break;
4509 795b88cb 2023-07-10 thomas case 'P':
4510 795b88cb 2023-07-10 thomas force_path = 1;
4511 795b88cb 2023-07-10 thomas break;
4512 795b88cb 2023-07-10 thomas case 'r':
4513 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
4514 795b88cb 2023-07-10 thomas if (repo_path == NULL)
4515 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
4516 795b88cb 2023-07-10 thomas optarg);
4517 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
4518 795b88cb 2023-07-10 thomas rflag = 1;
4519 795b88cb 2023-07-10 thomas break;
4520 795b88cb 2023-07-10 thomas case 's':
4521 795b88cb 2023-07-10 thomas diff_staged = 1;
4522 795b88cb 2023-07-10 thomas break;
4523 795b88cb 2023-07-10 thomas case 'w':
4524 795b88cb 2023-07-10 thomas ignore_whitespace = 1;
4525 795b88cb 2023-07-10 thomas break;
4526 795b88cb 2023-07-10 thomas default:
4527 795b88cb 2023-07-10 thomas usage_diff();
4528 795b88cb 2023-07-10 thomas /* NOTREACHED */
4529 795b88cb 2023-07-10 thomas }
4530 795b88cb 2023-07-10 thomas }
4531 795b88cb 2023-07-10 thomas
4532 795b88cb 2023-07-10 thomas argc -= optind;
4533 795b88cb 2023-07-10 thomas argv += optind;
4534 795b88cb 2023-07-10 thomas
4535 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
4536 795b88cb 2023-07-10 thomas if (cwd == NULL) {
4537 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
4538 795b88cb 2023-07-10 thomas goto done;
4539 795b88cb 2023-07-10 thomas }
4540 795b88cb 2023-07-10 thomas
4541 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
4542 795b88cb 2023-07-10 thomas if (error != NULL)
4543 795b88cb 2023-07-10 thomas goto done;
4544 795b88cb 2023-07-10 thomas
4545 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4546 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
4547 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
4548 795b88cb 2023-07-10 thomas goto done;
4549 795b88cb 2023-07-10 thomas else
4550 795b88cb 2023-07-10 thomas error = NULL;
4551 795b88cb 2023-07-10 thomas if (worktree) {
4552 795b88cb 2023-07-10 thomas repo_path =
4553 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
4554 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4555 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4556 795b88cb 2023-07-10 thomas goto done;
4557 795b88cb 2023-07-10 thomas }
4558 795b88cb 2023-07-10 thomas } else {
4559 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
4560 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
4561 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4562 795b88cb 2023-07-10 thomas goto done;
4563 795b88cb 2023-07-10 thomas }
4564 795b88cb 2023-07-10 thomas }
4565 795b88cb 2023-07-10 thomas }
4566 795b88cb 2023-07-10 thomas
4567 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4568 795b88cb 2023-07-10 thomas free(repo_path);
4569 795b88cb 2023-07-10 thomas if (error != NULL)
4570 795b88cb 2023-07-10 thomas goto done;
4571 795b88cb 2023-07-10 thomas
4572 795b88cb 2023-07-10 thomas if (show_diffstat) {
4573 795b88cb 2023-07-10 thomas dsa.paths = &diffstat_paths;
4574 795b88cb 2023-07-10 thomas dsa.force_text = force_text_diff;
4575 795b88cb 2023-07-10 thomas dsa.ignore_ws = ignore_whitespace;
4576 795b88cb 2023-07-10 thomas dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4577 795b88cb 2023-07-10 thomas }
4578 795b88cb 2023-07-10 thomas
4579 795b88cb 2023-07-10 thomas if (rflag || worktree == NULL || ncommit_args > 0) {
4580 795b88cb 2023-07-10 thomas if (force_path) {
4581 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4582 795b88cb 2023-07-10 thomas "-P option can only be used when diffing "
4583 795b88cb 2023-07-10 thomas "a work tree");
4584 795b88cb 2023-07-10 thomas goto done;
4585 795b88cb 2023-07-10 thomas }
4586 795b88cb 2023-07-10 thomas if (diff_staged) {
4587 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_NOT_IMPL,
4588 795b88cb 2023-07-10 thomas "-s option can only be used when diffing "
4589 795b88cb 2023-07-10 thomas "a work tree");
4590 795b88cb 2023-07-10 thomas goto done;
4591 795b88cb 2023-07-10 thomas }
4592 795b88cb 2023-07-10 thomas }
4593 795b88cb 2023-07-10 thomas
4594 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
4595 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
4596 795b88cb 2023-07-10 thomas if (error)
4597 795b88cb 2023-07-10 thomas goto done;
4598 795b88cb 2023-07-10 thomas
4599 795b88cb 2023-07-10 thomas if ((!force_path && argc == 2) || ncommit_args > 0) {
4600 795b88cb 2023-07-10 thomas int obj_type = (ncommit_args > 0 ?
4601 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4602 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4603 795b88cb 2023-07-10 thomas NULL);
4604 795b88cb 2023-07-10 thomas if (error)
4605 795b88cb 2023-07-10 thomas goto done;
4606 795b88cb 2023-07-10 thomas for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4607 795b88cb 2023-07-10 thomas const char *arg;
4608 795b88cb 2023-07-10 thomas if (ncommit_args > 0)
4609 795b88cb 2023-07-10 thomas arg = commit_args[i];
4610 795b88cb 2023-07-10 thomas else
4611 795b88cb 2023-07-10 thomas arg = argv[i];
4612 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&ids[i], &labels[i],
4613 795b88cb 2023-07-10 thomas arg, obj_type, &refs, repo);
4614 795b88cb 2023-07-10 thomas if (error) {
4615 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_REF &&
4616 795b88cb 2023-07-10 thomas error->code != GOT_ERR_NO_OBJ)
4617 795b88cb 2023-07-10 thomas goto done;
4618 795b88cb 2023-07-10 thomas if (ncommit_args > 0)
4619 795b88cb 2023-07-10 thomas goto done;
4620 795b88cb 2023-07-10 thomas error = NULL;
4621 795b88cb 2023-07-10 thomas break;
4622 795b88cb 2023-07-10 thomas }
4623 795b88cb 2023-07-10 thomas }
4624 795b88cb 2023-07-10 thomas }
4625 795b88cb 2023-07-10 thomas
4626 795b88cb 2023-07-10 thomas f1 = got_opentemp();
4627 795b88cb 2023-07-10 thomas if (f1 == NULL) {
4628 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4629 795b88cb 2023-07-10 thomas goto done;
4630 795b88cb 2023-07-10 thomas }
4631 795b88cb 2023-07-10 thomas
4632 795b88cb 2023-07-10 thomas f2 = got_opentemp();
4633 795b88cb 2023-07-10 thomas if (f2 == NULL) {
4634 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4635 795b88cb 2023-07-10 thomas goto done;
4636 795b88cb 2023-07-10 thomas }
4637 795b88cb 2023-07-10 thomas
4638 795b88cb 2023-07-10 thomas outfile = got_opentemp();
4639 795b88cb 2023-07-10 thomas if (outfile == NULL) {
4640 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
4641 795b88cb 2023-07-10 thomas goto done;
4642 795b88cb 2023-07-10 thomas }
4643 795b88cb 2023-07-10 thomas
4644 795b88cb 2023-07-10 thomas if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4645 795b88cb 2023-07-10 thomas struct print_diff_arg arg;
4646 795b88cb 2023-07-10 thomas char *id_str;
4647 795b88cb 2023-07-10 thomas
4648 795b88cb 2023-07-10 thomas if (worktree == NULL) {
4649 795b88cb 2023-07-10 thomas if (argc == 2 && ids[0] == NULL) {
4650 795b88cb 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4651 795b88cb 2023-07-10 thomas goto done;
4652 795b88cb 2023-07-10 thomas } else if (argc == 2 && ids[1] == NULL) {
4653 795b88cb 2023-07-10 thomas error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4654 795b88cb 2023-07-10 thomas goto done;
4655 795b88cb 2023-07-10 thomas } else if (argc > 0) {
4656 795b88cb 2023-07-10 thomas error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4657 795b88cb 2023-07-10 thomas "%s", "specified paths cannot be resolved");
4658 795b88cb 2023-07-10 thomas goto done;
4659 795b88cb 2023-07-10 thomas } else {
4660 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_NOT_WORKTREE);
4661 795b88cb 2023-07-10 thomas goto done;
4662 795b88cb 2023-07-10 thomas }
4663 795b88cb 2023-07-10 thomas }
4664 795b88cb 2023-07-10 thomas
4665 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
4666 795b88cb 2023-07-10 thomas worktree);
4667 795b88cb 2023-07-10 thomas if (error)
4668 795b88cb 2023-07-10 thomas goto done;
4669 795b88cb 2023-07-10 thomas
4670 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str,
4671 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
4672 795b88cb 2023-07-10 thomas if (error)
4673 795b88cb 2023-07-10 thomas goto done;
4674 795b88cb 2023-07-10 thomas arg.repo = repo;
4675 795b88cb 2023-07-10 thomas arg.worktree = worktree;
4676 795b88cb 2023-07-10 thomas arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
4677 795b88cb 2023-07-10 thomas arg.diff_context = diff_context;
4678 795b88cb 2023-07-10 thomas arg.id_str = id_str;
4679 795b88cb 2023-07-10 thomas arg.header_shown = 0;
4680 795b88cb 2023-07-10 thomas arg.diff_staged = diff_staged;
4681 795b88cb 2023-07-10 thomas arg.ignore_whitespace = ignore_whitespace;
4682 795b88cb 2023-07-10 thomas arg.force_text_diff = force_text_diff;
4683 795b88cb 2023-07-10 thomas arg.diffstat = show_diffstat ? &dsa : NULL;
4684 795b88cb 2023-07-10 thomas arg.f1 = f1;
4685 795b88cb 2023-07-10 thomas arg.f2 = f2;
4686 795b88cb 2023-07-10 thomas arg.outfile = outfile;
4687 795b88cb 2023-07-10 thomas
4688 795b88cb 2023-07-10 thomas error = got_worktree_status(worktree, &paths, repo, 0,
4689 795b88cb 2023-07-10 thomas print_diff, &arg, check_cancelled, NULL);
4690 795b88cb 2023-07-10 thomas free(id_str);
4691 795b88cb 2023-07-10 thomas if (error)
4692 795b88cb 2023-07-10 thomas goto done;
4693 795b88cb 2023-07-10 thomas
4694 795b88cb 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
4695 795b88cb 2023-07-10 thomas char *header;
4696 795b88cb 2023-07-10 thomas
4697 795b88cb 2023-07-10 thomas if (asprintf(&header, "diffstat %s%s",
4698 795b88cb 2023-07-10 thomas diff_staged ? "-s " : "",
4699 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree)) == -1) {
4700 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4701 795b88cb 2023-07-10 thomas goto done;
4702 795b88cb 2023-07-10 thomas }
4703 795b88cb 2023-07-10 thomas
4704 795b88cb 2023-07-10 thomas error = print_diffstat(&dsa, header);
4705 795b88cb 2023-07-10 thomas free(header);
4706 795b88cb 2023-07-10 thomas if (error)
4707 795b88cb 2023-07-10 thomas goto done;
4708 795b88cb 2023-07-10 thomas }
4709 795b88cb 2023-07-10 thomas
4710 795b88cb 2023-07-10 thomas error = printfile(outfile);
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 if (ncommit_args == 1) {
4715 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
4716 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, ids[0]);
4717 795b88cb 2023-07-10 thomas if (error)
4718 795b88cb 2023-07-10 thomas goto done;
4719 795b88cb 2023-07-10 thomas
4720 795b88cb 2023-07-10 thomas labels[1] = labels[0];
4721 795b88cb 2023-07-10 thomas ids[1] = ids[0];
4722 795b88cb 2023-07-10 thomas if (got_object_commit_get_nparents(commit) > 0) {
4723 795b88cb 2023-07-10 thomas const struct got_object_id_queue *pids;
4724 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
4725 795b88cb 2023-07-10 thomas pids = got_object_commit_get_parent_ids(commit);
4726 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(pids);
4727 795b88cb 2023-07-10 thomas ids[0] = got_object_id_dup(&pid->id);
4728 795b88cb 2023-07-10 thomas if (ids[0] == NULL) {
4729 795b88cb 2023-07-10 thomas error = got_error_from_errno(
4730 795b88cb 2023-07-10 thomas "got_object_id_dup");
4731 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4732 795b88cb 2023-07-10 thomas goto done;
4733 795b88cb 2023-07-10 thomas }
4734 795b88cb 2023-07-10 thomas error = got_object_id_str(&labels[0], ids[0]);
4735 795b88cb 2023-07-10 thomas if (error) {
4736 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4737 795b88cb 2023-07-10 thomas goto done;
4738 795b88cb 2023-07-10 thomas }
4739 795b88cb 2023-07-10 thomas } else {
4740 795b88cb 2023-07-10 thomas ids[0] = NULL;
4741 795b88cb 2023-07-10 thomas labels[0] = strdup("/dev/null");
4742 795b88cb 2023-07-10 thomas if (labels[0] == NULL) {
4743 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
4744 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4745 795b88cb 2023-07-10 thomas goto done;
4746 795b88cb 2023-07-10 thomas }
4747 795b88cb 2023-07-10 thomas }
4748 795b88cb 2023-07-10 thomas
4749 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
4750 795b88cb 2023-07-10 thomas }
4751 795b88cb 2023-07-10 thomas
4752 795b88cb 2023-07-10 thomas if (ncommit_args == 0 && argc > 2) {
4753 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
4754 795b88cb 2023-07-10 thomas "path arguments cannot be used when diffing two objects");
4755 795b88cb 2023-07-10 thomas goto done;
4756 795b88cb 2023-07-10 thomas }
4757 795b88cb 2023-07-10 thomas
4758 795b88cb 2023-07-10 thomas if (ids[0]) {
4759 795b88cb 2023-07-10 thomas error = got_object_get_type(&type1, repo, ids[0]);
4760 795b88cb 2023-07-10 thomas if (error)
4761 795b88cb 2023-07-10 thomas goto done;
4762 795b88cb 2023-07-10 thomas }
4763 795b88cb 2023-07-10 thomas
4764 795b88cb 2023-07-10 thomas error = got_object_get_type(&type2, repo, ids[1]);
4765 795b88cb 2023-07-10 thomas if (error)
4766 795b88cb 2023-07-10 thomas goto done;
4767 795b88cb 2023-07-10 thomas if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4768 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
4769 795b88cb 2023-07-10 thomas goto done;
4770 795b88cb 2023-07-10 thomas }
4771 795b88cb 2023-07-10 thomas if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
4772 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_OBJ_TYPE,
4773 795b88cb 2023-07-10 thomas "path arguments cannot be used when diffing blobs");
4774 795b88cb 2023-07-10 thomas goto done;
4775 795b88cb 2023-07-10 thomas }
4776 795b88cb 2023-07-10 thomas
4777 795b88cb 2023-07-10 thomas for (i = 0; ncommit_args > 0 && i < argc; i++) {
4778 795b88cb 2023-07-10 thomas char *in_repo_path;
4779 795b88cb 2023-07-10 thomas struct got_pathlist_entry *new;
4780 795b88cb 2023-07-10 thomas if (worktree) {
4781 795b88cb 2023-07-10 thomas const char *prefix;
4782 795b88cb 2023-07-10 thomas char *p;
4783 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree,
4784 795b88cb 2023-07-10 thomas argv[i]);
4785 795b88cb 2023-07-10 thomas if (error)
4786 795b88cb 2023-07-10 thomas goto done;
4787 795b88cb 2023-07-10 thomas prefix = got_worktree_get_path_prefix(worktree);
4788 795b88cb 2023-07-10 thomas while (prefix[0] == '/')
4789 795b88cb 2023-07-10 thomas prefix++;
4790 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
4791 795b88cb 2023-07-10 thomas (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4792 795b88cb 2023-07-10 thomas p) == -1) {
4793 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4794 795b88cb 2023-07-10 thomas free(p);
4795 795b88cb 2023-07-10 thomas goto done;
4796 795b88cb 2023-07-10 thomas }
4797 795b88cb 2023-07-10 thomas free(p);
4798 795b88cb 2023-07-10 thomas } else {
4799 795b88cb 2023-07-10 thomas char *mapped_path, *s;
4800 795b88cb 2023-07-10 thomas error = got_repo_map_path(&mapped_path, repo, argv[i]);
4801 795b88cb 2023-07-10 thomas if (error)
4802 795b88cb 2023-07-10 thomas goto done;
4803 795b88cb 2023-07-10 thomas s = mapped_path;
4804 795b88cb 2023-07-10 thomas while (s[0] == '/')
4805 795b88cb 2023-07-10 thomas s++;
4806 795b88cb 2023-07-10 thomas in_repo_path = strdup(s);
4807 795b88cb 2023-07-10 thomas if (in_repo_path == NULL) {
4808 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4809 795b88cb 2023-07-10 thomas free(mapped_path);
4810 795b88cb 2023-07-10 thomas goto done;
4811 795b88cb 2023-07-10 thomas }
4812 795b88cb 2023-07-10 thomas free(mapped_path);
4813 795b88cb 2023-07-10 thomas
4814 795b88cb 2023-07-10 thomas }
4815 795b88cb 2023-07-10 thomas error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4816 795b88cb 2023-07-10 thomas if (error || new == NULL /* duplicate */)
4817 795b88cb 2023-07-10 thomas free(in_repo_path);
4818 795b88cb 2023-07-10 thomas if (error)
4819 795b88cb 2023-07-10 thomas goto done;
4820 795b88cb 2023-07-10 thomas }
4821 795b88cb 2023-07-10 thomas
4822 795b88cb 2023-07-10 thomas if (worktree) {
4823 795b88cb 2023-07-10 thomas /* Release work tree lock. */
4824 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4825 795b88cb 2023-07-10 thomas worktree = NULL;
4826 795b88cb 2023-07-10 thomas }
4827 795b88cb 2023-07-10 thomas
4828 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
4829 795b88cb 2023-07-10 thomas if (fd1 == -1) {
4830 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
4831 795b88cb 2023-07-10 thomas goto done;
4832 795b88cb 2023-07-10 thomas }
4833 795b88cb 2023-07-10 thomas
4834 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
4835 795b88cb 2023-07-10 thomas if (fd2 == -1) {
4836 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
4837 795b88cb 2023-07-10 thomas goto done;
4838 795b88cb 2023-07-10 thomas }
4839 795b88cb 2023-07-10 thomas
4840 795b88cb 2023-07-10 thomas switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4841 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
4842 795b88cb 2023-07-10 thomas error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4843 795b88cb 2023-07-10 thomas fd1, fd2, ids[0], ids[1], NULL, NULL,
4844 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4845 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4846 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4847 795b88cb 2023-07-10 thomas break;
4848 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
4849 795b88cb 2023-07-10 thomas error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
4850 795b88cb 2023-07-10 thomas ids[0], ids[1], &paths, "", "",
4851 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4852 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4853 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4854 795b88cb 2023-07-10 thomas break;
4855 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
4856 795b88cb 2023-07-10 thomas fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
4857 795b88cb 2023-07-10 thomas error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
4858 795b88cb 2023-07-10 thomas fd1, fd2, ids[0], ids[1], &paths,
4859 795b88cb 2023-07-10 thomas GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
4860 795b88cb 2023-07-10 thomas ignore_whitespace, force_text_diff,
4861 795b88cb 2023-07-10 thomas show_diffstat ? &dsa : NULL, repo, outfile);
4862 795b88cb 2023-07-10 thomas break;
4863 795b88cb 2023-07-10 thomas default:
4864 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
4865 795b88cb 2023-07-10 thomas }
4866 795b88cb 2023-07-10 thomas if (error)
4867 795b88cb 2023-07-10 thomas goto done;
4868 795b88cb 2023-07-10 thomas
4869 795b88cb 2023-07-10 thomas if (show_diffstat && dsa.nfiles > 0) {
4870 795b88cb 2023-07-10 thomas char *header = NULL;
4871 795b88cb 2023-07-10 thomas
4872 795b88cb 2023-07-10 thomas if (asprintf(&header, "diffstat %s %s",
4873 795b88cb 2023-07-10 thomas labels[0], labels[1]) == -1) {
4874 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
4875 795b88cb 2023-07-10 thomas goto done;
4876 795b88cb 2023-07-10 thomas }
4877 795b88cb 2023-07-10 thomas
4878 795b88cb 2023-07-10 thomas error = print_diffstat(&dsa, header);
4879 795b88cb 2023-07-10 thomas free(header);
4880 795b88cb 2023-07-10 thomas if (error)
4881 795b88cb 2023-07-10 thomas goto done;
4882 795b88cb 2023-07-10 thomas }
4883 795b88cb 2023-07-10 thomas
4884 795b88cb 2023-07-10 thomas error = printfile(outfile);
4885 795b88cb 2023-07-10 thomas
4886 795b88cb 2023-07-10 thomas done:
4887 795b88cb 2023-07-10 thomas free(labels[0]);
4888 795b88cb 2023-07-10 thomas free(labels[1]);
4889 795b88cb 2023-07-10 thomas free(ids[0]);
4890 795b88cb 2023-07-10 thomas free(ids[1]);
4891 795b88cb 2023-07-10 thomas if (worktree)
4892 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
4893 795b88cb 2023-07-10 thomas if (repo) {
4894 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
4895 795b88cb 2023-07-10 thomas if (error == NULL)
4896 795b88cb 2023-07-10 thomas error = close_err;
4897 795b88cb 2023-07-10 thomas }
4898 795b88cb 2023-07-10 thomas if (pack_fds) {
4899 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
4900 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
4901 795b88cb 2023-07-10 thomas if (error == NULL)
4902 795b88cb 2023-07-10 thomas error = pack_err;
4903 795b88cb 2023-07-10 thomas }
4904 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
4905 795b88cb 2023-07-10 thomas got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
4906 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
4907 795b88cb 2023-07-10 thomas if (outfile && fclose(outfile) == EOF && error == NULL)
4908 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4909 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
4910 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4911 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
4912 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
4913 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
4914 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
4915 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
4916 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
4917 795b88cb 2023-07-10 thomas return error;
4918 795b88cb 2023-07-10 thomas }
4919 795b88cb 2023-07-10 thomas
4920 795b88cb 2023-07-10 thomas __dead static void
4921 795b88cb 2023-07-10 thomas usage_blame(void)
4922 795b88cb 2023-07-10 thomas {
4923 795b88cb 2023-07-10 thomas fprintf(stderr,
4924 795b88cb 2023-07-10 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
4925 795b88cb 2023-07-10 thomas getprogname());
4926 795b88cb 2023-07-10 thomas exit(1);
4927 795b88cb 2023-07-10 thomas }
4928 795b88cb 2023-07-10 thomas
4929 795b88cb 2023-07-10 thomas struct blame_line {
4930 795b88cb 2023-07-10 thomas int annotated;
4931 795b88cb 2023-07-10 thomas char *id_str;
4932 795b88cb 2023-07-10 thomas char *committer;
4933 795b88cb 2023-07-10 thomas char datebuf[11]; /* YYYY-MM-DD + NUL */
4934 795b88cb 2023-07-10 thomas };
4935 795b88cb 2023-07-10 thomas
4936 795b88cb 2023-07-10 thomas struct blame_cb_args {
4937 795b88cb 2023-07-10 thomas struct blame_line *lines;
4938 795b88cb 2023-07-10 thomas int nlines;
4939 795b88cb 2023-07-10 thomas int nlines_prec;
4940 795b88cb 2023-07-10 thomas int lineno_cur;
4941 795b88cb 2023-07-10 thomas off_t *line_offsets;
4942 795b88cb 2023-07-10 thomas FILE *f;
4943 795b88cb 2023-07-10 thomas struct got_repository *repo;
4944 795b88cb 2023-07-10 thomas };
4945 795b88cb 2023-07-10 thomas
4946 795b88cb 2023-07-10 thomas static const struct got_error *
4947 795b88cb 2023-07-10 thomas blame_cb(void *arg, int nlines, int lineno,
4948 795b88cb 2023-07-10 thomas struct got_commit_object *commit, struct got_object_id *id)
4949 795b88cb 2023-07-10 thomas {
4950 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
4951 795b88cb 2023-07-10 thomas struct blame_cb_args *a = arg;
4952 795b88cb 2023-07-10 thomas struct blame_line *bline;
4953 795b88cb 2023-07-10 thomas char *line = NULL;
4954 795b88cb 2023-07-10 thomas size_t linesize = 0;
4955 795b88cb 2023-07-10 thomas off_t offset;
4956 795b88cb 2023-07-10 thomas struct tm tm;
4957 795b88cb 2023-07-10 thomas time_t committer_time;
4958 795b88cb 2023-07-10 thomas
4959 795b88cb 2023-07-10 thomas if (nlines != a->nlines ||
4960 795b88cb 2023-07-10 thomas (lineno != -1 && lineno < 1) || lineno > a->nlines)
4961 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_RANGE);
4962 795b88cb 2023-07-10 thomas
4963 795b88cb 2023-07-10 thomas if (sigint_received)
4964 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_ITER_COMPLETED);
4965 795b88cb 2023-07-10 thomas
4966 795b88cb 2023-07-10 thomas if (lineno == -1)
4967 795b88cb 2023-07-10 thomas return NULL; /* no change in this commit */
4968 795b88cb 2023-07-10 thomas
4969 795b88cb 2023-07-10 thomas /* Annotate this line. */
4970 795b88cb 2023-07-10 thomas bline = &a->lines[lineno - 1];
4971 795b88cb 2023-07-10 thomas if (bline->annotated)
4972 795b88cb 2023-07-10 thomas return NULL;
4973 795b88cb 2023-07-10 thomas err = got_object_id_str(&bline->id_str, id);
4974 795b88cb 2023-07-10 thomas if (err)
4975 795b88cb 2023-07-10 thomas return err;
4976 795b88cb 2023-07-10 thomas
4977 795b88cb 2023-07-10 thomas bline->committer = strdup(got_object_commit_get_committer(commit));
4978 795b88cb 2023-07-10 thomas if (bline->committer == NULL) {
4979 795b88cb 2023-07-10 thomas err = got_error_from_errno("strdup");
4980 795b88cb 2023-07-10 thomas goto done;
4981 795b88cb 2023-07-10 thomas }
4982 795b88cb 2023-07-10 thomas
4983 795b88cb 2023-07-10 thomas committer_time = got_object_commit_get_committer_time(commit);
4984 795b88cb 2023-07-10 thomas if (gmtime_r(&committer_time, &tm) == NULL)
4985 795b88cb 2023-07-10 thomas return got_error_from_errno("gmtime_r");
4986 795b88cb 2023-07-10 thomas if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4987 795b88cb 2023-07-10 thomas &tm) == 0) {
4988 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_NO_SPACE);
4989 795b88cb 2023-07-10 thomas goto done;
4990 795b88cb 2023-07-10 thomas }
4991 795b88cb 2023-07-10 thomas bline->annotated = 1;
4992 795b88cb 2023-07-10 thomas
4993 795b88cb 2023-07-10 thomas /* Print lines annotated so far. */
4994 795b88cb 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
4995 795b88cb 2023-07-10 thomas if (!bline->annotated)
4996 795b88cb 2023-07-10 thomas goto done;
4997 795b88cb 2023-07-10 thomas
4998 795b88cb 2023-07-10 thomas offset = a->line_offsets[a->lineno_cur - 1];
4999 795b88cb 2023-07-10 thomas if (fseeko(a->f, offset, SEEK_SET) == -1) {
5000 795b88cb 2023-07-10 thomas err = got_error_from_errno("fseeko");
5001 795b88cb 2023-07-10 thomas goto done;
5002 795b88cb 2023-07-10 thomas }
5003 795b88cb 2023-07-10 thomas
5004 795b88cb 2023-07-10 thomas while (a->lineno_cur <= a->nlines && bline->annotated) {
5005 795b88cb 2023-07-10 thomas char *smallerthan, *at, *nl, *committer;
5006 795b88cb 2023-07-10 thomas size_t len;
5007 795b88cb 2023-07-10 thomas
5008 795b88cb 2023-07-10 thomas if (getline(&line, &linesize, a->f) == -1) {
5009 795b88cb 2023-07-10 thomas if (ferror(a->f))
5010 795b88cb 2023-07-10 thomas err = got_error_from_errno("getline");
5011 795b88cb 2023-07-10 thomas break;
5012 795b88cb 2023-07-10 thomas }
5013 795b88cb 2023-07-10 thomas
5014 795b88cb 2023-07-10 thomas committer = bline->committer;
5015 795b88cb 2023-07-10 thomas smallerthan = strchr(committer, '<');
5016 795b88cb 2023-07-10 thomas if (smallerthan && smallerthan[1] != '\0')
5017 795b88cb 2023-07-10 thomas committer = smallerthan + 1;
5018 795b88cb 2023-07-10 thomas at = strchr(committer, '@');
5019 795b88cb 2023-07-10 thomas if (at)
5020 795b88cb 2023-07-10 thomas *at = '\0';
5021 795b88cb 2023-07-10 thomas len = strlen(committer);
5022 795b88cb 2023-07-10 thomas if (len >= 9)
5023 795b88cb 2023-07-10 thomas committer[8] = '\0';
5024 795b88cb 2023-07-10 thomas
5025 795b88cb 2023-07-10 thomas nl = strchr(line, '\n');
5026 795b88cb 2023-07-10 thomas if (nl)
5027 795b88cb 2023-07-10 thomas *nl = '\0';
5028 795b88cb 2023-07-10 thomas printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5029 795b88cb 2023-07-10 thomas bline->id_str, bline->datebuf, committer, line);
5030 795b88cb 2023-07-10 thomas
5031 795b88cb 2023-07-10 thomas a->lineno_cur++;
5032 795b88cb 2023-07-10 thomas bline = &a->lines[a->lineno_cur - 1];
5033 795b88cb 2023-07-10 thomas }
5034 795b88cb 2023-07-10 thomas done:
5035 795b88cb 2023-07-10 thomas free(line);
5036 795b88cb 2023-07-10 thomas return err;
5037 795b88cb 2023-07-10 thomas }
5038 795b88cb 2023-07-10 thomas
5039 795b88cb 2023-07-10 thomas static const struct got_error *
5040 795b88cb 2023-07-10 thomas cmd_blame(int argc, char *argv[])
5041 795b88cb 2023-07-10 thomas {
5042 795b88cb 2023-07-10 thomas const struct got_error *error;
5043 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5044 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5045 795b88cb 2023-07-10 thomas char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5046 795b88cb 2023-07-10 thomas char *link_target = NULL;
5047 795b88cb 2023-07-10 thomas struct got_object_id *obj_id = NULL;
5048 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5049 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5050 795b88cb 2023-07-10 thomas struct got_blob_object *blob = NULL;
5051 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
5052 795b88cb 2023-07-10 thomas struct blame_cb_args bca;
5053 795b88cb 2023-07-10 thomas int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5054 795b88cb 2023-07-10 thomas off_t filesize;
5055 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5056 795b88cb 2023-07-10 thomas FILE *f1 = NULL, *f2 = NULL;
5057 795b88cb 2023-07-10 thomas
5058 795b88cb 2023-07-10 thomas fd1 = got_opentempfd();
5059 795b88cb 2023-07-10 thomas if (fd1 == -1)
5060 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
5061 795b88cb 2023-07-10 thomas
5062 795b88cb 2023-07-10 thomas memset(&bca, 0, sizeof(bca));
5063 795b88cb 2023-07-10 thomas
5064 795b88cb 2023-07-10 thomas #ifndef PROFILE
5065 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5066 795b88cb 2023-07-10 thomas NULL) == -1)
5067 795b88cb 2023-07-10 thomas err(1, "pledge");
5068 795b88cb 2023-07-10 thomas #endif
5069 795b88cb 2023-07-10 thomas
5070 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5071 795b88cb 2023-07-10 thomas switch (ch) {
5072 795b88cb 2023-07-10 thomas case 'c':
5073 795b88cb 2023-07-10 thomas commit_id_str = optarg;
5074 795b88cb 2023-07-10 thomas break;
5075 795b88cb 2023-07-10 thomas case 'r':
5076 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5077 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5078 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
5079 795b88cb 2023-07-10 thomas optarg);
5080 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5081 795b88cb 2023-07-10 thomas break;
5082 795b88cb 2023-07-10 thomas default:
5083 795b88cb 2023-07-10 thomas usage_blame();
5084 795b88cb 2023-07-10 thomas /* NOTREACHED */
5085 795b88cb 2023-07-10 thomas }
5086 795b88cb 2023-07-10 thomas }
5087 795b88cb 2023-07-10 thomas
5088 795b88cb 2023-07-10 thomas argc -= optind;
5089 795b88cb 2023-07-10 thomas argv += optind;
5090 795b88cb 2023-07-10 thomas
5091 795b88cb 2023-07-10 thomas if (argc == 1)
5092 795b88cb 2023-07-10 thomas path = argv[0];
5093 795b88cb 2023-07-10 thomas else
5094 795b88cb 2023-07-10 thomas usage_blame();
5095 795b88cb 2023-07-10 thomas
5096 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5097 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5098 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5099 795b88cb 2023-07-10 thomas goto done;
5100 795b88cb 2023-07-10 thomas }
5101 795b88cb 2023-07-10 thomas
5102 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5103 795b88cb 2023-07-10 thomas if (error != NULL)
5104 795b88cb 2023-07-10 thomas goto done;
5105 795b88cb 2023-07-10 thomas
5106 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5107 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5108 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5109 795b88cb 2023-07-10 thomas goto done;
5110 795b88cb 2023-07-10 thomas else
5111 795b88cb 2023-07-10 thomas error = NULL;
5112 795b88cb 2023-07-10 thomas if (worktree) {
5113 795b88cb 2023-07-10 thomas repo_path =
5114 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5115 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5116 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5117 795b88cb 2023-07-10 thomas if (error)
5118 795b88cb 2023-07-10 thomas goto done;
5119 795b88cb 2023-07-10 thomas }
5120 795b88cb 2023-07-10 thomas } else {
5121 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
5122 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5123 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5124 795b88cb 2023-07-10 thomas goto done;
5125 795b88cb 2023-07-10 thomas }
5126 795b88cb 2023-07-10 thomas }
5127 795b88cb 2023-07-10 thomas }
5128 795b88cb 2023-07-10 thomas
5129 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5130 795b88cb 2023-07-10 thomas if (error != NULL)
5131 795b88cb 2023-07-10 thomas goto done;
5132 795b88cb 2023-07-10 thomas
5133 795b88cb 2023-07-10 thomas if (worktree) {
5134 795b88cb 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5135 795b88cb 2023-07-10 thomas char *p;
5136 795b88cb 2023-07-10 thomas
5137 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5138 795b88cb 2023-07-10 thomas if (error)
5139 795b88cb 2023-07-10 thomas goto done;
5140 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5141 795b88cb 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5142 795b88cb 2023-07-10 thomas p) == -1) {
5143 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
5144 795b88cb 2023-07-10 thomas free(p);
5145 795b88cb 2023-07-10 thomas goto done;
5146 795b88cb 2023-07-10 thomas }
5147 795b88cb 2023-07-10 thomas free(p);
5148 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5149 795b88cb 2023-07-10 thomas } else {
5150 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5151 795b88cb 2023-07-10 thomas if (error)
5152 795b88cb 2023-07-10 thomas goto done;
5153 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5154 795b88cb 2023-07-10 thomas }
5155 795b88cb 2023-07-10 thomas if (error)
5156 795b88cb 2023-07-10 thomas goto done;
5157 795b88cb 2023-07-10 thomas
5158 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
5159 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
5160 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, worktree ?
5161 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5162 795b88cb 2023-07-10 thomas if (error != NULL)
5163 795b88cb 2023-07-10 thomas goto done;
5164 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5165 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
5166 795b88cb 2023-07-10 thomas if (error != NULL)
5167 795b88cb 2023-07-10 thomas goto done;
5168 795b88cb 2023-07-10 thomas } else {
5169 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5170 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5171 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5172 795b88cb 2023-07-10 thomas NULL);
5173 795b88cb 2023-07-10 thomas if (error)
5174 795b88cb 2023-07-10 thomas goto done;
5175 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5176 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5177 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
5178 795b88cb 2023-07-10 thomas if (error)
5179 795b88cb 2023-07-10 thomas goto done;
5180 795b88cb 2023-07-10 thomas }
5181 795b88cb 2023-07-10 thomas
5182 795b88cb 2023-07-10 thomas if (worktree) {
5183 795b88cb 2023-07-10 thomas /* Release work tree lock. */
5184 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5185 795b88cb 2023-07-10 thomas worktree = NULL;
5186 795b88cb 2023-07-10 thomas }
5187 795b88cb 2023-07-10 thomas
5188 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5189 795b88cb 2023-07-10 thomas if (error)
5190 795b88cb 2023-07-10 thomas goto done;
5191 795b88cb 2023-07-10 thomas
5192 795b88cb 2023-07-10 thomas error = got_object_resolve_symlinks(&link_target, in_repo_path,
5193 795b88cb 2023-07-10 thomas commit, repo);
5194 795b88cb 2023-07-10 thomas if (error)
5195 795b88cb 2023-07-10 thomas goto done;
5196 795b88cb 2023-07-10 thomas
5197 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&obj_id, repo, commit,
5198 795b88cb 2023-07-10 thomas link_target ? link_target : in_repo_path);
5199 795b88cb 2023-07-10 thomas if (error)
5200 795b88cb 2023-07-10 thomas goto done;
5201 795b88cb 2023-07-10 thomas
5202 795b88cb 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, obj_id);
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 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5207 795b88cb 2023-07-10 thomas error = got_error_path(link_target ? link_target : in_repo_path,
5208 795b88cb 2023-07-10 thomas GOT_ERR_OBJ_TYPE);
5209 795b88cb 2023-07-10 thomas goto done;
5210 795b88cb 2023-07-10 thomas }
5211 795b88cb 2023-07-10 thomas
5212 795b88cb 2023-07-10 thomas error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5213 795b88cb 2023-07-10 thomas if (error)
5214 795b88cb 2023-07-10 thomas goto done;
5215 795b88cb 2023-07-10 thomas bca.f = got_opentemp();
5216 795b88cb 2023-07-10 thomas if (bca.f == NULL) {
5217 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5218 795b88cb 2023-07-10 thomas goto done;
5219 795b88cb 2023-07-10 thomas }
5220 795b88cb 2023-07-10 thomas error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5221 795b88cb 2023-07-10 thomas &bca.line_offsets, bca.f, blob);
5222 795b88cb 2023-07-10 thomas if (error || bca.nlines == 0)
5223 795b88cb 2023-07-10 thomas goto done;
5224 795b88cb 2023-07-10 thomas
5225 795b88cb 2023-07-10 thomas /* Don't include \n at EOF in the blame line count. */
5226 795b88cb 2023-07-10 thomas if (bca.line_offsets[bca.nlines - 1] == filesize)
5227 795b88cb 2023-07-10 thomas bca.nlines--;
5228 795b88cb 2023-07-10 thomas
5229 795b88cb 2023-07-10 thomas bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5230 795b88cb 2023-07-10 thomas if (bca.lines == NULL) {
5231 795b88cb 2023-07-10 thomas error = got_error_from_errno("calloc");
5232 795b88cb 2023-07-10 thomas goto done;
5233 795b88cb 2023-07-10 thomas }
5234 795b88cb 2023-07-10 thomas bca.lineno_cur = 1;
5235 795b88cb 2023-07-10 thomas bca.nlines_prec = 0;
5236 795b88cb 2023-07-10 thomas i = bca.nlines;
5237 795b88cb 2023-07-10 thomas while (i > 0) {
5238 795b88cb 2023-07-10 thomas i /= 10;
5239 795b88cb 2023-07-10 thomas bca.nlines_prec++;
5240 795b88cb 2023-07-10 thomas }
5241 795b88cb 2023-07-10 thomas bca.repo = repo;
5242 795b88cb 2023-07-10 thomas
5243 795b88cb 2023-07-10 thomas fd2 = got_opentempfd();
5244 795b88cb 2023-07-10 thomas if (fd2 == -1) {
5245 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5246 795b88cb 2023-07-10 thomas goto done;
5247 795b88cb 2023-07-10 thomas }
5248 795b88cb 2023-07-10 thomas fd3 = got_opentempfd();
5249 795b88cb 2023-07-10 thomas if (fd3 == -1) {
5250 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentempfd");
5251 795b88cb 2023-07-10 thomas goto done;
5252 795b88cb 2023-07-10 thomas }
5253 795b88cb 2023-07-10 thomas f1 = got_opentemp();
5254 795b88cb 2023-07-10 thomas if (f1 == NULL) {
5255 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5256 795b88cb 2023-07-10 thomas goto done;
5257 795b88cb 2023-07-10 thomas }
5258 795b88cb 2023-07-10 thomas f2 = got_opentemp();
5259 795b88cb 2023-07-10 thomas if (f2 == NULL) {
5260 795b88cb 2023-07-10 thomas error = got_error_from_errno("got_opentemp");
5261 795b88cb 2023-07-10 thomas goto done;
5262 795b88cb 2023-07-10 thomas }
5263 795b88cb 2023-07-10 thomas error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5264 795b88cb 2023-07-10 thomas repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5265 795b88cb 2023-07-10 thomas check_cancelled, NULL, fd2, fd3, f1, f2);
5266 795b88cb 2023-07-10 thomas done:
5267 795b88cb 2023-07-10 thomas free(in_repo_path);
5268 795b88cb 2023-07-10 thomas free(link_target);
5269 795b88cb 2023-07-10 thomas free(repo_path);
5270 795b88cb 2023-07-10 thomas free(cwd);
5271 795b88cb 2023-07-10 thomas free(commit_id);
5272 795b88cb 2023-07-10 thomas free(obj_id);
5273 795b88cb 2023-07-10 thomas if (commit)
5274 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
5275 795b88cb 2023-07-10 thomas
5276 795b88cb 2023-07-10 thomas if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5277 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5278 795b88cb 2023-07-10 thomas if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5279 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5280 795b88cb 2023-07-10 thomas if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5281 795b88cb 2023-07-10 thomas error = got_error_from_errno("close");
5282 795b88cb 2023-07-10 thomas if (f1 && fclose(f1) == EOF && error == NULL)
5283 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5284 795b88cb 2023-07-10 thomas if (f2 && fclose(f2) == EOF && error == NULL)
5285 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5286 795b88cb 2023-07-10 thomas
5287 795b88cb 2023-07-10 thomas if (blob)
5288 795b88cb 2023-07-10 thomas got_object_blob_close(blob);
5289 795b88cb 2023-07-10 thomas if (worktree)
5290 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5291 795b88cb 2023-07-10 thomas if (repo) {
5292 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5293 795b88cb 2023-07-10 thomas if (error == NULL)
5294 795b88cb 2023-07-10 thomas error = close_err;
5295 795b88cb 2023-07-10 thomas }
5296 795b88cb 2023-07-10 thomas if (pack_fds) {
5297 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5298 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5299 795b88cb 2023-07-10 thomas if (error == NULL)
5300 795b88cb 2023-07-10 thomas error = pack_err;
5301 795b88cb 2023-07-10 thomas }
5302 795b88cb 2023-07-10 thomas if (bca.lines) {
5303 795b88cb 2023-07-10 thomas for (i = 0; i < bca.nlines; i++) {
5304 795b88cb 2023-07-10 thomas struct blame_line *bline = &bca.lines[i];
5305 795b88cb 2023-07-10 thomas free(bline->id_str);
5306 795b88cb 2023-07-10 thomas free(bline->committer);
5307 795b88cb 2023-07-10 thomas }
5308 795b88cb 2023-07-10 thomas free(bca.lines);
5309 795b88cb 2023-07-10 thomas }
5310 795b88cb 2023-07-10 thomas free(bca.line_offsets);
5311 795b88cb 2023-07-10 thomas if (bca.f && fclose(bca.f) == EOF && error == NULL)
5312 795b88cb 2023-07-10 thomas error = got_error_from_errno("fclose");
5313 795b88cb 2023-07-10 thomas return error;
5314 795b88cb 2023-07-10 thomas }
5315 795b88cb 2023-07-10 thomas
5316 795b88cb 2023-07-10 thomas __dead static void
5317 795b88cb 2023-07-10 thomas usage_tree(void)
5318 795b88cb 2023-07-10 thomas {
5319 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5320 795b88cb 2023-07-10 thomas "[path]\n", getprogname());
5321 795b88cb 2023-07-10 thomas exit(1);
5322 795b88cb 2023-07-10 thomas }
5323 795b88cb 2023-07-10 thomas
5324 795b88cb 2023-07-10 thomas static const struct got_error *
5325 795b88cb 2023-07-10 thomas print_entry(struct got_tree_entry *te, const char *id, const char *path,
5326 795b88cb 2023-07-10 thomas const char *root_path, struct got_repository *repo)
5327 795b88cb 2023-07-10 thomas {
5328 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5329 795b88cb 2023-07-10 thomas int is_root_path = (strcmp(path, root_path) == 0);
5330 795b88cb 2023-07-10 thomas const char *modestr = "";
5331 795b88cb 2023-07-10 thomas mode_t mode = got_tree_entry_get_mode(te);
5332 795b88cb 2023-07-10 thomas char *link_target = NULL;
5333 795b88cb 2023-07-10 thomas
5334 795b88cb 2023-07-10 thomas path += strlen(root_path);
5335 795b88cb 2023-07-10 thomas while (path[0] == '/')
5336 795b88cb 2023-07-10 thomas path++;
5337 795b88cb 2023-07-10 thomas
5338 795b88cb 2023-07-10 thomas if (got_object_tree_entry_is_submodule(te))
5339 795b88cb 2023-07-10 thomas modestr = "$";
5340 795b88cb 2023-07-10 thomas else if (S_ISLNK(mode)) {
5341 795b88cb 2023-07-10 thomas int i;
5342 795b88cb 2023-07-10 thomas
5343 795b88cb 2023-07-10 thomas err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5344 795b88cb 2023-07-10 thomas if (err)
5345 795b88cb 2023-07-10 thomas return err;
5346 795b88cb 2023-07-10 thomas for (i = 0; link_target[i] != '\0'; i++) {
5347 795b88cb 2023-07-10 thomas if (!isprint((unsigned char)link_target[i]))
5348 795b88cb 2023-07-10 thomas link_target[i] = '?';
5349 795b88cb 2023-07-10 thomas }
5350 795b88cb 2023-07-10 thomas
5351 795b88cb 2023-07-10 thomas modestr = "@";
5352 795b88cb 2023-07-10 thomas }
5353 795b88cb 2023-07-10 thomas else if (S_ISDIR(mode))
5354 795b88cb 2023-07-10 thomas modestr = "/";
5355 795b88cb 2023-07-10 thomas else if (mode & S_IXUSR)
5356 795b88cb 2023-07-10 thomas modestr = "*";
5357 795b88cb 2023-07-10 thomas
5358 795b88cb 2023-07-10 thomas printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5359 795b88cb 2023-07-10 thomas is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5360 795b88cb 2023-07-10 thomas link_target ? " -> ": "", link_target ? link_target : "");
5361 795b88cb 2023-07-10 thomas
5362 795b88cb 2023-07-10 thomas free(link_target);
5363 795b88cb 2023-07-10 thomas return NULL;
5364 795b88cb 2023-07-10 thomas }
5365 795b88cb 2023-07-10 thomas
5366 795b88cb 2023-07-10 thomas static const struct got_error *
5367 795b88cb 2023-07-10 thomas print_tree(const char *path, struct got_commit_object *commit,
5368 795b88cb 2023-07-10 thomas int show_ids, int recurse, const char *root_path,
5369 795b88cb 2023-07-10 thomas struct got_repository *repo)
5370 795b88cb 2023-07-10 thomas {
5371 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5372 795b88cb 2023-07-10 thomas struct got_object_id *tree_id = NULL;
5373 795b88cb 2023-07-10 thomas struct got_tree_object *tree = NULL;
5374 795b88cb 2023-07-10 thomas int nentries, i;
5375 795b88cb 2023-07-10 thomas
5376 795b88cb 2023-07-10 thomas err = got_object_id_by_path(&tree_id, repo, commit, path);
5377 795b88cb 2023-07-10 thomas if (err)
5378 795b88cb 2023-07-10 thomas goto done;
5379 795b88cb 2023-07-10 thomas
5380 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, tree_id);
5381 795b88cb 2023-07-10 thomas if (err)
5382 795b88cb 2023-07-10 thomas goto done;
5383 795b88cb 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
5384 795b88cb 2023-07-10 thomas for (i = 0; i < nentries; i++) {
5385 795b88cb 2023-07-10 thomas struct got_tree_entry *te;
5386 795b88cb 2023-07-10 thomas char *id = NULL;
5387 795b88cb 2023-07-10 thomas
5388 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
5389 795b88cb 2023-07-10 thomas break;
5390 795b88cb 2023-07-10 thomas
5391 795b88cb 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
5392 795b88cb 2023-07-10 thomas if (show_ids) {
5393 795b88cb 2023-07-10 thomas char *id_str;
5394 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str,
5395 795b88cb 2023-07-10 thomas got_tree_entry_get_id(te));
5396 795b88cb 2023-07-10 thomas if (err)
5397 795b88cb 2023-07-10 thomas goto done;
5398 795b88cb 2023-07-10 thomas if (asprintf(&id, "%s ", id_str) == -1) {
5399 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5400 795b88cb 2023-07-10 thomas free(id_str);
5401 795b88cb 2023-07-10 thomas goto done;
5402 795b88cb 2023-07-10 thomas }
5403 795b88cb 2023-07-10 thomas free(id_str);
5404 795b88cb 2023-07-10 thomas }
5405 795b88cb 2023-07-10 thomas err = print_entry(te, id, path, root_path, repo);
5406 795b88cb 2023-07-10 thomas free(id);
5407 795b88cb 2023-07-10 thomas if (err)
5408 795b88cb 2023-07-10 thomas goto done;
5409 795b88cb 2023-07-10 thomas
5410 795b88cb 2023-07-10 thomas if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5411 795b88cb 2023-07-10 thomas char *child_path;
5412 795b88cb 2023-07-10 thomas if (asprintf(&child_path, "%s%s%s", path,
5413 795b88cb 2023-07-10 thomas path[0] == '/' && path[1] == '\0' ? "" : "/",
5414 795b88cb 2023-07-10 thomas got_tree_entry_get_name(te)) == -1) {
5415 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5416 795b88cb 2023-07-10 thomas goto done;
5417 795b88cb 2023-07-10 thomas }
5418 795b88cb 2023-07-10 thomas err = print_tree(child_path, commit, show_ids, 1,
5419 795b88cb 2023-07-10 thomas root_path, repo);
5420 795b88cb 2023-07-10 thomas free(child_path);
5421 795b88cb 2023-07-10 thomas if (err)
5422 795b88cb 2023-07-10 thomas goto done;
5423 795b88cb 2023-07-10 thomas }
5424 795b88cb 2023-07-10 thomas }
5425 795b88cb 2023-07-10 thomas done:
5426 795b88cb 2023-07-10 thomas if (tree)
5427 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
5428 795b88cb 2023-07-10 thomas free(tree_id);
5429 795b88cb 2023-07-10 thomas return err;
5430 795b88cb 2023-07-10 thomas }
5431 795b88cb 2023-07-10 thomas
5432 795b88cb 2023-07-10 thomas static const struct got_error *
5433 795b88cb 2023-07-10 thomas cmd_tree(int argc, char *argv[])
5434 795b88cb 2023-07-10 thomas {
5435 795b88cb 2023-07-10 thomas const struct got_error *error;
5436 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5437 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5438 795b88cb 2023-07-10 thomas const char *path, *refname = NULL;
5439 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5440 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
5441 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5442 795b88cb 2023-07-10 thomas char *commit_id_str = NULL;
5443 795b88cb 2023-07-10 thomas int show_ids = 0, recurse = 0;
5444 795b88cb 2023-07-10 thomas int ch;
5445 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5446 795b88cb 2023-07-10 thomas
5447 795b88cb 2023-07-10 thomas #ifndef PROFILE
5448 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5449 795b88cb 2023-07-10 thomas NULL) == -1)
5450 795b88cb 2023-07-10 thomas err(1, "pledge");
5451 795b88cb 2023-07-10 thomas #endif
5452 795b88cb 2023-07-10 thomas
5453 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
5454 795b88cb 2023-07-10 thomas switch (ch) {
5455 795b88cb 2023-07-10 thomas case 'c':
5456 795b88cb 2023-07-10 thomas commit_id_str = optarg;
5457 795b88cb 2023-07-10 thomas break;
5458 795b88cb 2023-07-10 thomas case 'i':
5459 795b88cb 2023-07-10 thomas show_ids = 1;
5460 795b88cb 2023-07-10 thomas break;
5461 795b88cb 2023-07-10 thomas case 'R':
5462 795b88cb 2023-07-10 thomas recurse = 1;
5463 795b88cb 2023-07-10 thomas break;
5464 795b88cb 2023-07-10 thomas case 'r':
5465 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
5466 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5467 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
5468 795b88cb 2023-07-10 thomas optarg);
5469 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
5470 795b88cb 2023-07-10 thomas break;
5471 795b88cb 2023-07-10 thomas default:
5472 795b88cb 2023-07-10 thomas usage_tree();
5473 795b88cb 2023-07-10 thomas /* NOTREACHED */
5474 795b88cb 2023-07-10 thomas }
5475 795b88cb 2023-07-10 thomas }
5476 795b88cb 2023-07-10 thomas
5477 795b88cb 2023-07-10 thomas argc -= optind;
5478 795b88cb 2023-07-10 thomas argv += optind;
5479 795b88cb 2023-07-10 thomas
5480 795b88cb 2023-07-10 thomas if (argc == 1)
5481 795b88cb 2023-07-10 thomas path = argv[0];
5482 795b88cb 2023-07-10 thomas else if (argc > 1)
5483 795b88cb 2023-07-10 thomas usage_tree();
5484 795b88cb 2023-07-10 thomas else
5485 795b88cb 2023-07-10 thomas path = NULL;
5486 795b88cb 2023-07-10 thomas
5487 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5488 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5489 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5490 795b88cb 2023-07-10 thomas goto done;
5491 795b88cb 2023-07-10 thomas }
5492 795b88cb 2023-07-10 thomas
5493 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5494 795b88cb 2023-07-10 thomas if (error != NULL)
5495 795b88cb 2023-07-10 thomas goto done;
5496 795b88cb 2023-07-10 thomas
5497 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5498 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5499 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
5500 795b88cb 2023-07-10 thomas goto done;
5501 795b88cb 2023-07-10 thomas else
5502 795b88cb 2023-07-10 thomas error = NULL;
5503 795b88cb 2023-07-10 thomas if (worktree) {
5504 795b88cb 2023-07-10 thomas repo_path =
5505 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
5506 795b88cb 2023-07-10 thomas if (repo_path == NULL)
5507 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5508 795b88cb 2023-07-10 thomas if (error)
5509 795b88cb 2023-07-10 thomas goto done;
5510 795b88cb 2023-07-10 thomas } else {
5511 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
5512 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
5513 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
5514 795b88cb 2023-07-10 thomas goto done;
5515 795b88cb 2023-07-10 thomas }
5516 795b88cb 2023-07-10 thomas }
5517 795b88cb 2023-07-10 thomas }
5518 795b88cb 2023-07-10 thomas
5519 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5520 795b88cb 2023-07-10 thomas if (error != NULL)
5521 795b88cb 2023-07-10 thomas goto done;
5522 795b88cb 2023-07-10 thomas
5523 795b88cb 2023-07-10 thomas if (worktree) {
5524 795b88cb 2023-07-10 thomas const char *prefix = got_worktree_get_path_prefix(worktree);
5525 795b88cb 2023-07-10 thomas char *p;
5526 795b88cb 2023-07-10 thomas
5527 795b88cb 2023-07-10 thomas if (path == NULL || got_path_is_root_dir(path))
5528 795b88cb 2023-07-10 thomas path = "";
5529 795b88cb 2023-07-10 thomas error = got_worktree_resolve_path(&p, worktree, path);
5530 795b88cb 2023-07-10 thomas if (error)
5531 795b88cb 2023-07-10 thomas goto done;
5532 795b88cb 2023-07-10 thomas if (asprintf(&in_repo_path, "%s%s%s", prefix,
5533 795b88cb 2023-07-10 thomas (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5534 795b88cb 2023-07-10 thomas p) == -1) {
5535 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
5536 795b88cb 2023-07-10 thomas free(p);
5537 795b88cb 2023-07-10 thomas goto done;
5538 795b88cb 2023-07-10 thomas }
5539 795b88cb 2023-07-10 thomas free(p);
5540 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5541 795b88cb 2023-07-10 thomas if (error)
5542 795b88cb 2023-07-10 thomas goto done;
5543 795b88cb 2023-07-10 thomas } else {
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 if (path == NULL)
5548 795b88cb 2023-07-10 thomas path = "/";
5549 795b88cb 2023-07-10 thomas error = got_repo_map_path(&in_repo_path, repo, path);
5550 795b88cb 2023-07-10 thomas if (error != NULL)
5551 795b88cb 2023-07-10 thomas goto done;
5552 795b88cb 2023-07-10 thomas }
5553 795b88cb 2023-07-10 thomas
5554 795b88cb 2023-07-10 thomas if (commit_id_str == NULL) {
5555 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
5556 795b88cb 2023-07-10 thomas if (worktree)
5557 795b88cb 2023-07-10 thomas refname = got_worktree_get_head_ref_name(worktree);
5558 795b88cb 2023-07-10 thomas else
5559 795b88cb 2023-07-10 thomas refname = GOT_REF_HEAD;
5560 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo, refname, 0);
5561 795b88cb 2023-07-10 thomas if (error != NULL)
5562 795b88cb 2023-07-10 thomas goto done;
5563 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
5564 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
5565 795b88cb 2023-07-10 thomas if (error != NULL)
5566 795b88cb 2023-07-10 thomas goto done;
5567 795b88cb 2023-07-10 thomas } else {
5568 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5569 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5570 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5571 795b88cb 2023-07-10 thomas NULL);
5572 795b88cb 2023-07-10 thomas if (error)
5573 795b88cb 2023-07-10 thomas goto done;
5574 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
5575 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5576 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
5577 795b88cb 2023-07-10 thomas if (error)
5578 795b88cb 2023-07-10 thomas goto done;
5579 795b88cb 2023-07-10 thomas }
5580 795b88cb 2023-07-10 thomas
5581 795b88cb 2023-07-10 thomas if (worktree) {
5582 795b88cb 2023-07-10 thomas /* Release work tree lock. */
5583 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5584 795b88cb 2023-07-10 thomas worktree = NULL;
5585 795b88cb 2023-07-10 thomas }
5586 795b88cb 2023-07-10 thomas
5587 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5588 795b88cb 2023-07-10 thomas if (error)
5589 795b88cb 2023-07-10 thomas goto done;
5590 795b88cb 2023-07-10 thomas
5591 795b88cb 2023-07-10 thomas error = print_tree(in_repo_path, commit, show_ids, recurse,
5592 795b88cb 2023-07-10 thomas in_repo_path, repo);
5593 795b88cb 2023-07-10 thomas done:
5594 795b88cb 2023-07-10 thomas free(in_repo_path);
5595 795b88cb 2023-07-10 thomas free(repo_path);
5596 795b88cb 2023-07-10 thomas free(cwd);
5597 795b88cb 2023-07-10 thomas free(commit_id);
5598 795b88cb 2023-07-10 thomas if (commit)
5599 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
5600 795b88cb 2023-07-10 thomas if (worktree)
5601 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
5602 795b88cb 2023-07-10 thomas if (repo) {
5603 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5604 795b88cb 2023-07-10 thomas if (error == NULL)
5605 795b88cb 2023-07-10 thomas error = close_err;
5606 795b88cb 2023-07-10 thomas }
5607 795b88cb 2023-07-10 thomas if (pack_fds) {
5608 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5609 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5610 795b88cb 2023-07-10 thomas if (error == NULL)
5611 795b88cb 2023-07-10 thomas error = pack_err;
5612 795b88cb 2023-07-10 thomas }
5613 795b88cb 2023-07-10 thomas return error;
5614 795b88cb 2023-07-10 thomas }
5615 795b88cb 2023-07-10 thomas
5616 795b88cb 2023-07-10 thomas __dead static void
5617 795b88cb 2023-07-10 thomas usage_status(void)
5618 795b88cb 2023-07-10 thomas {
5619 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
5620 795b88cb 2023-07-10 thomas "[-s status-codes] [path ...]\n", getprogname());
5621 795b88cb 2023-07-10 thomas exit(1);
5622 795b88cb 2023-07-10 thomas }
5623 795b88cb 2023-07-10 thomas
5624 795b88cb 2023-07-10 thomas struct got_status_arg {
5625 795b88cb 2023-07-10 thomas char *status_codes;
5626 795b88cb 2023-07-10 thomas int suppress;
5627 795b88cb 2023-07-10 thomas };
5628 795b88cb 2023-07-10 thomas
5629 795b88cb 2023-07-10 thomas static const struct got_error *
5630 795b88cb 2023-07-10 thomas print_status(void *arg, unsigned char status, unsigned char staged_status,
5631 795b88cb 2023-07-10 thomas const char *path, struct got_object_id *blob_id,
5632 795b88cb 2023-07-10 thomas struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5633 795b88cb 2023-07-10 thomas int dirfd, const char *de_name)
5634 795b88cb 2023-07-10 thomas {
5635 795b88cb 2023-07-10 thomas struct got_status_arg *st = arg;
5636 795b88cb 2023-07-10 thomas
5637 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
5638 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
5639 795b88cb 2023-07-10 thomas if (st != NULL && st->status_codes) {
5640 795b88cb 2023-07-10 thomas size_t ncodes = strlen(st->status_codes);
5641 795b88cb 2023-07-10 thomas int i, j = 0;
5642 795b88cb 2023-07-10 thomas
5643 795b88cb 2023-07-10 thomas for (i = 0; i < ncodes ; i++) {
5644 795b88cb 2023-07-10 thomas if (st->suppress) {
5645 795b88cb 2023-07-10 thomas if (status == st->status_codes[i] ||
5646 795b88cb 2023-07-10 thomas staged_status == st->status_codes[i]) {
5647 795b88cb 2023-07-10 thomas j++;
5648 795b88cb 2023-07-10 thomas continue;
5649 795b88cb 2023-07-10 thomas }
5650 795b88cb 2023-07-10 thomas } else {
5651 795b88cb 2023-07-10 thomas if (status == st->status_codes[i] ||
5652 795b88cb 2023-07-10 thomas staged_status == st->status_codes[i])
5653 795b88cb 2023-07-10 thomas break;
5654 795b88cb 2023-07-10 thomas }
5655 795b88cb 2023-07-10 thomas }
5656 795b88cb 2023-07-10 thomas
5657 795b88cb 2023-07-10 thomas if (st->suppress && j == 0)
5658 795b88cb 2023-07-10 thomas goto print;
5659 795b88cb 2023-07-10 thomas
5660 795b88cb 2023-07-10 thomas if (i == ncodes)
5661 795b88cb 2023-07-10 thomas return NULL;
5662 795b88cb 2023-07-10 thomas }
5663 795b88cb 2023-07-10 thomas print:
5664 795b88cb 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
5665 795b88cb 2023-07-10 thomas return NULL;
5666 795b88cb 2023-07-10 thomas }
5667 795b88cb 2023-07-10 thomas
5668 795b88cb 2023-07-10 thomas static const struct got_error *
5669 795b88cb 2023-07-10 thomas cmd_status(int argc, char *argv[])
5670 795b88cb 2023-07-10 thomas {
5671 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
5672 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
5673 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
5674 795b88cb 2023-07-10 thomas struct got_status_arg st;
5675 795b88cb 2023-07-10 thomas char *cwd = NULL;
5676 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
5677 795b88cb 2023-07-10 thomas int ch, i, no_ignores = 0;
5678 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
5679 795b88cb 2023-07-10 thomas
5680 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
5681 795b88cb 2023-07-10 thomas
5682 795b88cb 2023-07-10 thomas memset(&st, 0, sizeof(st));
5683 795b88cb 2023-07-10 thomas st.status_codes = NULL;
5684 795b88cb 2023-07-10 thomas st.suppress = 0;
5685 795b88cb 2023-07-10 thomas
5686 795b88cb 2023-07-10 thomas #ifndef PROFILE
5687 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5688 795b88cb 2023-07-10 thomas NULL) == -1)
5689 795b88cb 2023-07-10 thomas err(1, "pledge");
5690 795b88cb 2023-07-10 thomas #endif
5691 795b88cb 2023-07-10 thomas
5692 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
5693 795b88cb 2023-07-10 thomas switch (ch) {
5694 795b88cb 2023-07-10 thomas case 'I':
5695 795b88cb 2023-07-10 thomas no_ignores = 1;
5696 795b88cb 2023-07-10 thomas break;
5697 795b88cb 2023-07-10 thomas case 'S':
5698 795b88cb 2023-07-10 thomas if (st.status_codes != NULL && st.suppress == 0)
5699 795b88cb 2023-07-10 thomas option_conflict('S', 's');
5700 795b88cb 2023-07-10 thomas st.suppress = 1;
5701 795b88cb 2023-07-10 thomas /* fallthrough */
5702 795b88cb 2023-07-10 thomas case 's':
5703 795b88cb 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
5704 795b88cb 2023-07-10 thomas switch (optarg[i]) {
5705 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
5706 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
5707 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
5708 795b88cb 2023-07-10 thomas case GOT_STATUS_CONFLICT:
5709 795b88cb 2023-07-10 thomas case GOT_STATUS_MISSING:
5710 795b88cb 2023-07-10 thomas case GOT_STATUS_OBSTRUCTED:
5711 795b88cb 2023-07-10 thomas case GOT_STATUS_UNVERSIONED:
5712 795b88cb 2023-07-10 thomas case GOT_STATUS_MODE_CHANGE:
5713 795b88cb 2023-07-10 thomas case GOT_STATUS_NONEXISTENT:
5714 795b88cb 2023-07-10 thomas break;
5715 795b88cb 2023-07-10 thomas default:
5716 795b88cb 2023-07-10 thomas errx(1, "invalid status code '%c'",
5717 795b88cb 2023-07-10 thomas optarg[i]);
5718 795b88cb 2023-07-10 thomas }
5719 795b88cb 2023-07-10 thomas }
5720 795b88cb 2023-07-10 thomas if (ch == 's' && st.suppress)
5721 795b88cb 2023-07-10 thomas option_conflict('s', 'S');
5722 795b88cb 2023-07-10 thomas st.status_codes = optarg;
5723 795b88cb 2023-07-10 thomas break;
5724 795b88cb 2023-07-10 thomas default:
5725 795b88cb 2023-07-10 thomas usage_status();
5726 795b88cb 2023-07-10 thomas /* NOTREACHED */
5727 795b88cb 2023-07-10 thomas }
5728 795b88cb 2023-07-10 thomas }
5729 795b88cb 2023-07-10 thomas
5730 795b88cb 2023-07-10 thomas argc -= optind;
5731 795b88cb 2023-07-10 thomas argv += optind;
5732 795b88cb 2023-07-10 thomas
5733 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
5734 795b88cb 2023-07-10 thomas if (cwd == NULL) {
5735 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
5736 795b88cb 2023-07-10 thomas goto done;
5737 795b88cb 2023-07-10 thomas }
5738 795b88cb 2023-07-10 thomas
5739 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
5740 795b88cb 2023-07-10 thomas if (error != NULL)
5741 795b88cb 2023-07-10 thomas goto done;
5742 795b88cb 2023-07-10 thomas
5743 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
5744 795b88cb 2023-07-10 thomas if (error) {
5745 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
5746 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "status", cwd);
5747 795b88cb 2023-07-10 thomas goto done;
5748 795b88cb 2023-07-10 thomas }
5749 795b88cb 2023-07-10 thomas
5750 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5751 795b88cb 2023-07-10 thomas NULL, pack_fds);
5752 795b88cb 2023-07-10 thomas if (error != NULL)
5753 795b88cb 2023-07-10 thomas goto done;
5754 795b88cb 2023-07-10 thomas
5755 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
5756 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
5757 795b88cb 2023-07-10 thomas if (error)
5758 795b88cb 2023-07-10 thomas goto done;
5759 795b88cb 2023-07-10 thomas
5760 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, 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 = got_worktree_status(worktree, &paths, repo, no_ignores,
5765 795b88cb 2023-07-10 thomas print_status, &st, check_cancelled, NULL);
5766 795b88cb 2023-07-10 thomas done:
5767 795b88cb 2023-07-10 thomas if (pack_fds) {
5768 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
5769 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
5770 795b88cb 2023-07-10 thomas if (error == NULL)
5771 795b88cb 2023-07-10 thomas error = pack_err;
5772 795b88cb 2023-07-10 thomas }
5773 795b88cb 2023-07-10 thomas if (repo) {
5774 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
5775 795b88cb 2023-07-10 thomas if (error == NULL)
5776 795b88cb 2023-07-10 thomas error = close_err;
5777 795b88cb 2023-07-10 thomas }
5778 795b88cb 2023-07-10 thomas
5779 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5780 795b88cb 2023-07-10 thomas free(cwd);
5781 795b88cb 2023-07-10 thomas return error;
5782 795b88cb 2023-07-10 thomas }
5783 795b88cb 2023-07-10 thomas
5784 795b88cb 2023-07-10 thomas __dead static void
5785 795b88cb 2023-07-10 thomas usage_tag(void)
5786 795b88cb 2023-07-10 thomas {
5787 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
5788 795b88cb 2023-07-10 thomas "[-r repository-path] [-s signer-id] name\n", getprogname());
5789 795b88cb 2023-07-10 thomas exit(1);
5790 795b88cb 2023-07-10 thomas }
5791 795b88cb 2023-07-10 thomas
5792 795b88cb 2023-07-10 thomas #if 0
5793 795b88cb 2023-07-10 thomas static const struct got_error *
5794 795b88cb 2023-07-10 thomas sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5795 795b88cb 2023-07-10 thomas {
5796 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
5797 795b88cb 2023-07-10 thomas struct got_reflist_entry *re, *se, *new;
5798 795b88cb 2023-07-10 thomas struct got_object_id *re_id, *se_id;
5799 795b88cb 2023-07-10 thomas struct got_tag_object *re_tag, *se_tag;
5800 795b88cb 2023-07-10 thomas time_t re_time, se_time;
5801 795b88cb 2023-07-10 thomas
5802 795b88cb 2023-07-10 thomas STAILQ_FOREACH(re, tags, entry) {
5803 795b88cb 2023-07-10 thomas se = STAILQ_FIRST(sorted);
5804 795b88cb 2023-07-10 thomas if (se == NULL) {
5805 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
5806 795b88cb 2023-07-10 thomas if (err)
5807 795b88cb 2023-07-10 thomas return err;
5808 795b88cb 2023-07-10 thomas STAILQ_INSERT_HEAD(sorted, new, entry);
5809 795b88cb 2023-07-10 thomas continue;
5810 795b88cb 2023-07-10 thomas } else {
5811 795b88cb 2023-07-10 thomas err = got_ref_resolve(&re_id, repo, re->ref);
5812 795b88cb 2023-07-10 thomas if (err)
5813 795b88cb 2023-07-10 thomas break;
5814 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&re_tag, repo, re_id);
5815 795b88cb 2023-07-10 thomas free(re_id);
5816 795b88cb 2023-07-10 thomas if (err)
5817 795b88cb 2023-07-10 thomas break;
5818 795b88cb 2023-07-10 thomas re_time = got_object_tag_get_tagger_time(re_tag);
5819 795b88cb 2023-07-10 thomas got_object_tag_close(re_tag);
5820 795b88cb 2023-07-10 thomas }
5821 795b88cb 2023-07-10 thomas
5822 795b88cb 2023-07-10 thomas while (se) {
5823 795b88cb 2023-07-10 thomas err = got_ref_resolve(&se_id, repo, re->ref);
5824 795b88cb 2023-07-10 thomas if (err)
5825 795b88cb 2023-07-10 thomas break;
5826 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&se_tag, repo, se_id);
5827 795b88cb 2023-07-10 thomas free(se_id);
5828 795b88cb 2023-07-10 thomas if (err)
5829 795b88cb 2023-07-10 thomas break;
5830 795b88cb 2023-07-10 thomas se_time = got_object_tag_get_tagger_time(se_tag);
5831 795b88cb 2023-07-10 thomas got_object_tag_close(se_tag);
5832 795b88cb 2023-07-10 thomas
5833 795b88cb 2023-07-10 thomas if (se_time > re_time) {
5834 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&new, re);
5835 795b88cb 2023-07-10 thomas if (err)
5836 795b88cb 2023-07-10 thomas return err;
5837 795b88cb 2023-07-10 thomas STAILQ_INSERT_AFTER(sorted, se, new, entry);
5838 795b88cb 2023-07-10 thomas break;
5839 795b88cb 2023-07-10 thomas }
5840 795b88cb 2023-07-10 thomas se = STAILQ_NEXT(se, entry);
5841 795b88cb 2023-07-10 thomas continue;
5842 795b88cb 2023-07-10 thomas }
5843 795b88cb 2023-07-10 thomas }
5844 795b88cb 2023-07-10 thomas done:
5845 795b88cb 2023-07-10 thomas return err;
5846 795b88cb 2023-07-10 thomas }
5847 795b88cb 2023-07-10 thomas #endif
5848 795b88cb 2023-07-10 thomas
5849 795b88cb 2023-07-10 thomas static const struct got_error *
5850 795b88cb 2023-07-10 thomas get_tag_refname(char **refname, const char *tag_name)
5851 795b88cb 2023-07-10 thomas {
5852 795b88cb 2023-07-10 thomas const struct got_error *err;
5853 795b88cb 2023-07-10 thomas
5854 795b88cb 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0) {
5855 795b88cb 2023-07-10 thomas *refname = strdup(tag_name);
5856 795b88cb 2023-07-10 thomas if (*refname == NULL)
5857 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
5858 795b88cb 2023-07-10 thomas } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
5859 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
5860 795b88cb 2023-07-10 thomas *refname = NULL;
5861 795b88cb 2023-07-10 thomas return err;
5862 795b88cb 2023-07-10 thomas }
5863 795b88cb 2023-07-10 thomas
5864 795b88cb 2023-07-10 thomas return NULL;
5865 795b88cb 2023-07-10 thomas }
5866 795b88cb 2023-07-10 thomas
5867 795b88cb 2023-07-10 thomas static const struct got_error *
5868 795b88cb 2023-07-10 thomas list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
5869 795b88cb 2023-07-10 thomas const char *allowed_signers, const char *revoked_signers, int verbosity)
5870 795b88cb 2023-07-10 thomas {
5871 795b88cb 2023-07-10 thomas static const struct got_error *err = NULL;
5872 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
5873 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
5874 795b88cb 2023-07-10 thomas char *wanted_refname = NULL;
5875 795b88cb 2023-07-10 thomas int bad_sigs = 0;
5876 795b88cb 2023-07-10 thomas
5877 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
5878 795b88cb 2023-07-10 thomas
5879 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5880 795b88cb 2023-07-10 thomas if (err)
5881 795b88cb 2023-07-10 thomas return err;
5882 795b88cb 2023-07-10 thomas
5883 795b88cb 2023-07-10 thomas if (tag_name) {
5884 795b88cb 2023-07-10 thomas struct got_reference *ref;
5885 795b88cb 2023-07-10 thomas err = get_tag_refname(&wanted_refname, tag_name);
5886 795b88cb 2023-07-10 thomas if (err)
5887 795b88cb 2023-07-10 thomas goto done;
5888 795b88cb 2023-07-10 thomas /* Wanted tag reference should exist. */
5889 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, wanted_refname, 0);
5890 795b88cb 2023-07-10 thomas if (err)
5891 795b88cb 2023-07-10 thomas goto done;
5892 795b88cb 2023-07-10 thomas got_ref_close(ref);
5893 795b88cb 2023-07-10 thomas }
5894 795b88cb 2023-07-10 thomas
5895 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
5896 795b88cb 2023-07-10 thomas const char *refname;
5897 795b88cb 2023-07-10 thomas char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5898 795b88cb 2023-07-10 thomas char datebuf[26];
5899 795b88cb 2023-07-10 thomas const char *tagger, *ssh_sig = NULL;
5900 795b88cb 2023-07-10 thomas char *sig_msg = NULL;
5901 795b88cb 2023-07-10 thomas time_t tagger_time;
5902 795b88cb 2023-07-10 thomas struct got_object_id *id;
5903 795b88cb 2023-07-10 thomas struct got_tag_object *tag;
5904 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
5905 795b88cb 2023-07-10 thomas
5906 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
5907 795b88cb 2023-07-10 thomas if (strncmp(refname, "refs/tags/", 10) != 0 ||
5908 795b88cb 2023-07-10 thomas (wanted_refname && strcmp(refname, wanted_refname) != 0))
5909 795b88cb 2023-07-10 thomas continue;
5910 795b88cb 2023-07-10 thomas refname += 10;
5911 795b88cb 2023-07-10 thomas refstr = got_ref_to_str(re->ref);
5912 795b88cb 2023-07-10 thomas if (refstr == NULL) {
5913 795b88cb 2023-07-10 thomas err = got_error_from_errno("got_ref_to_str");
5914 795b88cb 2023-07-10 thomas break;
5915 795b88cb 2023-07-10 thomas }
5916 795b88cb 2023-07-10 thomas
5917 795b88cb 2023-07-10 thomas err = got_ref_resolve(&id, repo, re->ref);
5918 795b88cb 2023-07-10 thomas if (err)
5919 795b88cb 2023-07-10 thomas break;
5920 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
5921 795b88cb 2023-07-10 thomas if (err) {
5922 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
5923 795b88cb 2023-07-10 thomas free(id);
5924 795b88cb 2023-07-10 thomas break;
5925 795b88cb 2023-07-10 thomas }
5926 795b88cb 2023-07-10 thomas /* "lightweight" tag */
5927 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
5928 795b88cb 2023-07-10 thomas if (err) {
5929 795b88cb 2023-07-10 thomas free(id);
5930 795b88cb 2023-07-10 thomas break;
5931 795b88cb 2023-07-10 thomas }
5932 795b88cb 2023-07-10 thomas tagger = got_object_commit_get_committer(commit);
5933 795b88cb 2023-07-10 thomas tagger_time =
5934 795b88cb 2023-07-10 thomas got_object_commit_get_committer_time(commit);
5935 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, id);
5936 795b88cb 2023-07-10 thomas free(id);
5937 795b88cb 2023-07-10 thomas if (err)
5938 795b88cb 2023-07-10 thomas break;
5939 795b88cb 2023-07-10 thomas } else {
5940 795b88cb 2023-07-10 thomas free(id);
5941 795b88cb 2023-07-10 thomas tagger = got_object_tag_get_tagger(tag);
5942 795b88cb 2023-07-10 thomas tagger_time = got_object_tag_get_tagger_time(tag);
5943 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str,
5944 795b88cb 2023-07-10 thomas got_object_tag_get_object_id(tag));
5945 795b88cb 2023-07-10 thomas if (err)
5946 795b88cb 2023-07-10 thomas break;
5947 795b88cb 2023-07-10 thomas }
5948 795b88cb 2023-07-10 thomas
5949 795b88cb 2023-07-10 thomas if (tag && verify_tags) {
5950 795b88cb 2023-07-10 thomas ssh_sig = got_sigs_get_tagmsg_ssh_signature(
5951 795b88cb 2023-07-10 thomas got_object_tag_get_message(tag));
5952 795b88cb 2023-07-10 thomas if (ssh_sig && allowed_signers == NULL) {
5953 795b88cb 2023-07-10 thomas err = got_error_msg(
5954 795b88cb 2023-07-10 thomas GOT_ERR_VERIFY_TAG_SIGNATURE,
5955 795b88cb 2023-07-10 thomas "SSH signature verification requires "
5956 795b88cb 2023-07-10 thomas "setting allowed_signers in "
5957 795b88cb 2023-07-10 thomas "got.conf(5)");
5958 795b88cb 2023-07-10 thomas break;
5959 795b88cb 2023-07-10 thomas }
5960 795b88cb 2023-07-10 thomas }
5961 795b88cb 2023-07-10 thomas
5962 795b88cb 2023-07-10 thomas printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5963 795b88cb 2023-07-10 thomas free(refstr);
5964 795b88cb 2023-07-10 thomas printf("from: %s\n", tagger);
5965 795b88cb 2023-07-10 thomas datestr = get_datestr(&tagger_time, datebuf);
5966 795b88cb 2023-07-10 thomas if (datestr)
5967 795b88cb 2023-07-10 thomas printf("date: %s UTC\n", datestr);
5968 795b88cb 2023-07-10 thomas if (commit)
5969 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5970 795b88cb 2023-07-10 thomas else {
5971 795b88cb 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
5972 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
5973 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5974 795b88cb 2023-07-10 thomas id_str);
5975 795b88cb 2023-07-10 thomas break;
5976 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
5977 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
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_COMMIT:
5981 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
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_TAG:
5985 795b88cb 2023-07-10 thomas printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5986 795b88cb 2023-07-10 thomas id_str);
5987 795b88cb 2023-07-10 thomas break;
5988 795b88cb 2023-07-10 thomas default:
5989 795b88cb 2023-07-10 thomas break;
5990 795b88cb 2023-07-10 thomas }
5991 795b88cb 2023-07-10 thomas }
5992 795b88cb 2023-07-10 thomas free(id_str);
5993 795b88cb 2023-07-10 thomas
5994 795b88cb 2023-07-10 thomas if (ssh_sig) {
5995 795b88cb 2023-07-10 thomas err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
5996 795b88cb 2023-07-10 thomas allowed_signers, revoked_signers, verbosity);
5997 795b88cb 2023-07-10 thomas if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
5998 795b88cb 2023-07-10 thomas bad_sigs = 1;
5999 795b88cb 2023-07-10 thomas else if (err)
6000 795b88cb 2023-07-10 thomas break;
6001 795b88cb 2023-07-10 thomas printf("signature: %s", sig_msg);
6002 795b88cb 2023-07-10 thomas free(sig_msg);
6003 795b88cb 2023-07-10 thomas sig_msg = NULL;
6004 795b88cb 2023-07-10 thomas }
6005 795b88cb 2023-07-10 thomas
6006 795b88cb 2023-07-10 thomas if (commit) {
6007 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&tagmsg0, commit);
6008 795b88cb 2023-07-10 thomas if (err)
6009 795b88cb 2023-07-10 thomas break;
6010 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
6011 795b88cb 2023-07-10 thomas } else {
6012 795b88cb 2023-07-10 thomas tagmsg0 = strdup(got_object_tag_get_message(tag));
6013 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
6014 795b88cb 2023-07-10 thomas if (tagmsg0 == NULL) {
6015 795b88cb 2023-07-10 thomas err = got_error_from_errno("strdup");
6016 795b88cb 2023-07-10 thomas break;
6017 795b88cb 2023-07-10 thomas }
6018 795b88cb 2023-07-10 thomas }
6019 795b88cb 2023-07-10 thomas
6020 795b88cb 2023-07-10 thomas tagmsg = tagmsg0;
6021 795b88cb 2023-07-10 thomas do {
6022 795b88cb 2023-07-10 thomas line = strsep(&tagmsg, "\n");
6023 795b88cb 2023-07-10 thomas if (line)
6024 795b88cb 2023-07-10 thomas printf(" %s\n", line);
6025 795b88cb 2023-07-10 thomas } while (line);
6026 795b88cb 2023-07-10 thomas free(tagmsg0);
6027 795b88cb 2023-07-10 thomas }
6028 795b88cb 2023-07-10 thomas done:
6029 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6030 795b88cb 2023-07-10 thomas free(wanted_refname);
6031 795b88cb 2023-07-10 thomas
6032 795b88cb 2023-07-10 thomas if (err == NULL && bad_sigs)
6033 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
6034 795b88cb 2023-07-10 thomas return err;
6035 795b88cb 2023-07-10 thomas }
6036 795b88cb 2023-07-10 thomas
6037 795b88cb 2023-07-10 thomas static const struct got_error *
6038 795b88cb 2023-07-10 thomas get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6039 795b88cb 2023-07-10 thomas const char *tag_name, const char *repo_path)
6040 795b88cb 2023-07-10 thomas {
6041 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6042 795b88cb 2023-07-10 thomas char *template = NULL, *initial_content = NULL;
6043 795b88cb 2023-07-10 thomas char *editor = NULL;
6044 795b88cb 2023-07-10 thomas int initial_content_len;
6045 795b88cb 2023-07-10 thomas int fd = -1;
6046 795b88cb 2023-07-10 thomas
6047 795b88cb 2023-07-10 thomas if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6048 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
6049 795b88cb 2023-07-10 thomas goto done;
6050 795b88cb 2023-07-10 thomas }
6051 795b88cb 2023-07-10 thomas
6052 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
6053 795b88cb 2023-07-10 thomas "\n# tagging commit %s as %s\n",
6054 795b88cb 2023-07-10 thomas commit_id_str, tag_name);
6055 795b88cb 2023-07-10 thomas if (initial_content_len == -1) {
6056 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
6057 795b88cb 2023-07-10 thomas goto done;
6058 795b88cb 2023-07-10 thomas }
6059 795b88cb 2023-07-10 thomas
6060 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
6061 795b88cb 2023-07-10 thomas if (err)
6062 795b88cb 2023-07-10 thomas goto done;
6063 795b88cb 2023-07-10 thomas
6064 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
6065 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", *tagmsg_path);
6066 795b88cb 2023-07-10 thomas goto done;
6067 795b88cb 2023-07-10 thomas }
6068 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
6069 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
6070 795b88cb 2023-07-10 thomas goto done;
6071 795b88cb 2023-07-10 thomas }
6072 795b88cb 2023-07-10 thomas fd = -1;
6073 795b88cb 2023-07-10 thomas
6074 795b88cb 2023-07-10 thomas err = get_editor(&editor);
6075 795b88cb 2023-07-10 thomas if (err)
6076 795b88cb 2023-07-10 thomas goto done;
6077 795b88cb 2023-07-10 thomas err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6078 795b88cb 2023-07-10 thomas initial_content_len, 1);
6079 795b88cb 2023-07-10 thomas done:
6080 795b88cb 2023-07-10 thomas free(initial_content);
6081 795b88cb 2023-07-10 thomas free(template);
6082 795b88cb 2023-07-10 thomas free(editor);
6083 795b88cb 2023-07-10 thomas
6084 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6085 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", *tagmsg_path);
6086 795b88cb 2023-07-10 thomas
6087 795b88cb 2023-07-10 thomas if (err) {
6088 795b88cb 2023-07-10 thomas free(*tagmsg);
6089 795b88cb 2023-07-10 thomas *tagmsg = NULL;
6090 795b88cb 2023-07-10 thomas }
6091 795b88cb 2023-07-10 thomas return err;
6092 795b88cb 2023-07-10 thomas }
6093 795b88cb 2023-07-10 thomas
6094 795b88cb 2023-07-10 thomas static const struct got_error *
6095 795b88cb 2023-07-10 thomas add_tag(struct got_repository *repo, const char *tagger,
6096 795b88cb 2023-07-10 thomas const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
6097 795b88cb 2023-07-10 thomas const char *signer_id, int verbosity)
6098 795b88cb 2023-07-10 thomas {
6099 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6100 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL, *tag_id = NULL;
6101 795b88cb 2023-07-10 thomas char *label = NULL, *commit_id_str = NULL;
6102 795b88cb 2023-07-10 thomas struct got_reference *ref = NULL;
6103 795b88cb 2023-07-10 thomas char *refname = NULL, *tagmsg = NULL;
6104 795b88cb 2023-07-10 thomas char *tagmsg_path = NULL, *tag_id_str = NULL;
6105 795b88cb 2023-07-10 thomas int preserve_tagmsg = 0;
6106 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
6107 795b88cb 2023-07-10 thomas
6108 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
6109 795b88cb 2023-07-10 thomas
6110 795b88cb 2023-07-10 thomas /*
6111 795b88cb 2023-07-10 thomas * Don't let the user create a tag name with a leading '-'.
6112 795b88cb 2023-07-10 thomas * While technically a valid reference name, this case is usually
6113 795b88cb 2023-07-10 thomas * an unintended typo.
6114 795b88cb 2023-07-10 thomas */
6115 795b88cb 2023-07-10 thomas if (tag_name[0] == '-')
6116 795b88cb 2023-07-10 thomas return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6117 795b88cb 2023-07-10 thomas
6118 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6119 795b88cb 2023-07-10 thomas if (err)
6120 795b88cb 2023-07-10 thomas goto done;
6121 795b88cb 2023-07-10 thomas
6122 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6123 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, &refs, repo);
6124 795b88cb 2023-07-10 thomas if (err)
6125 795b88cb 2023-07-10 thomas goto done;
6126 795b88cb 2023-07-10 thomas
6127 795b88cb 2023-07-10 thomas err = got_object_id_str(&commit_id_str, commit_id);
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 = get_tag_refname(&refname, tag_name);
6132 795b88cb 2023-07-10 thomas if (err)
6133 795b88cb 2023-07-10 thomas goto done;
6134 795b88cb 2023-07-10 thomas if (strncmp("refs/tags/", tag_name, 10) == 0)
6135 795b88cb 2023-07-10 thomas tag_name += 10;
6136 795b88cb 2023-07-10 thomas
6137 795b88cb 2023-07-10 thomas err = got_ref_open(&ref, repo, refname, 0);
6138 795b88cb 2023-07-10 thomas if (err == NULL) {
6139 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_TAG_EXISTS);
6140 795b88cb 2023-07-10 thomas goto done;
6141 795b88cb 2023-07-10 thomas } else if (err->code != GOT_ERR_NOT_REF)
6142 795b88cb 2023-07-10 thomas goto done;
6143 795b88cb 2023-07-10 thomas
6144 795b88cb 2023-07-10 thomas if (tagmsg_arg == NULL) {
6145 795b88cb 2023-07-10 thomas err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6146 795b88cb 2023-07-10 thomas tag_name, got_repo_get_path(repo));
6147 795b88cb 2023-07-10 thomas if (err) {
6148 795b88cb 2023-07-10 thomas if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6149 795b88cb 2023-07-10 thomas tagmsg_path != NULL)
6150 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6151 795b88cb 2023-07-10 thomas goto done;
6152 795b88cb 2023-07-10 thomas }
6153 795b88cb 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
6154 795b88cb 2023-07-10 thomas err = got_sigs_apply_unveil();
6155 795b88cb 2023-07-10 thomas if (err)
6156 795b88cb 2023-07-10 thomas goto done;
6157 795b88cb 2023-07-10 thomas err = apply_unveil(got_repo_get_path(repo), 0, NULL);
6158 795b88cb 2023-07-10 thomas if (err)
6159 795b88cb 2023-07-10 thomas goto done;
6160 795b88cb 2023-07-10 thomas }
6161 795b88cb 2023-07-10 thomas
6162 795b88cb 2023-07-10 thomas err = got_object_tag_create(&tag_id, tag_name, commit_id,
6163 795b88cb 2023-07-10 thomas tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
6164 795b88cb 2023-07-10 thomas verbosity);
6165 795b88cb 2023-07-10 thomas if (err) {
6166 795b88cb 2023-07-10 thomas if (tagmsg_path)
6167 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6168 795b88cb 2023-07-10 thomas goto done;
6169 795b88cb 2023-07-10 thomas }
6170 795b88cb 2023-07-10 thomas
6171 795b88cb 2023-07-10 thomas err = got_ref_alloc(&ref, refname, tag_id);
6172 795b88cb 2023-07-10 thomas if (err) {
6173 795b88cb 2023-07-10 thomas if (tagmsg_path)
6174 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6175 795b88cb 2023-07-10 thomas goto done;
6176 795b88cb 2023-07-10 thomas }
6177 795b88cb 2023-07-10 thomas
6178 795b88cb 2023-07-10 thomas err = got_ref_write(ref, repo);
6179 795b88cb 2023-07-10 thomas if (err) {
6180 795b88cb 2023-07-10 thomas if (tagmsg_path)
6181 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6182 795b88cb 2023-07-10 thomas goto done;
6183 795b88cb 2023-07-10 thomas }
6184 795b88cb 2023-07-10 thomas
6185 795b88cb 2023-07-10 thomas err = got_object_id_str(&tag_id_str, tag_id);
6186 795b88cb 2023-07-10 thomas if (err) {
6187 795b88cb 2023-07-10 thomas if (tagmsg_path)
6188 795b88cb 2023-07-10 thomas preserve_tagmsg = 1;
6189 795b88cb 2023-07-10 thomas goto done;
6190 795b88cb 2023-07-10 thomas }
6191 795b88cb 2023-07-10 thomas printf("Created tag %s\n", tag_id_str);
6192 795b88cb 2023-07-10 thomas done:
6193 795b88cb 2023-07-10 thomas if (preserve_tagmsg) {
6194 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: tag message preserved in %s\n",
6195 795b88cb 2023-07-10 thomas getprogname(), tagmsg_path);
6196 795b88cb 2023-07-10 thomas } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6197 795b88cb 2023-07-10 thomas err = got_error_from_errno2("unlink", tagmsg_path);
6198 795b88cb 2023-07-10 thomas free(tag_id_str);
6199 795b88cb 2023-07-10 thomas if (ref)
6200 795b88cb 2023-07-10 thomas got_ref_close(ref);
6201 795b88cb 2023-07-10 thomas free(commit_id);
6202 795b88cb 2023-07-10 thomas free(commit_id_str);
6203 795b88cb 2023-07-10 thomas free(refname);
6204 795b88cb 2023-07-10 thomas free(tagmsg);
6205 795b88cb 2023-07-10 thomas free(tagmsg_path);
6206 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6207 795b88cb 2023-07-10 thomas return err;
6208 795b88cb 2023-07-10 thomas }
6209 795b88cb 2023-07-10 thomas
6210 795b88cb 2023-07-10 thomas static const struct got_error *
6211 795b88cb 2023-07-10 thomas cmd_tag(int argc, char *argv[])
6212 795b88cb 2023-07-10 thomas {
6213 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6214 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6215 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6216 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6217 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *tagger = NULL;
6218 795b88cb 2023-07-10 thomas char *allowed_signers = NULL, *revoked_signers = NULL;
6219 795b88cb 2023-07-10 thomas const char *signer_id = NULL;
6220 795b88cb 2023-07-10 thomas const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
6221 795b88cb 2023-07-10 thomas int ch, do_list = 0, verify_tags = 0, verbosity = 0;
6222 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6223 795b88cb 2023-07-10 thomas
6224 795b88cb 2023-07-10 thomas #ifndef PROFILE
6225 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6226 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
6227 795b88cb 2023-07-10 thomas err(1, "pledge");
6228 795b88cb 2023-07-10 thomas #endif
6229 795b88cb 2023-07-10 thomas
6230 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
6231 795b88cb 2023-07-10 thomas switch (ch) {
6232 795b88cb 2023-07-10 thomas case 'c':
6233 795b88cb 2023-07-10 thomas commit_id_arg = optarg;
6234 795b88cb 2023-07-10 thomas break;
6235 795b88cb 2023-07-10 thomas case 'l':
6236 795b88cb 2023-07-10 thomas do_list = 1;
6237 795b88cb 2023-07-10 thomas break;
6238 795b88cb 2023-07-10 thomas case 'm':
6239 795b88cb 2023-07-10 thomas tagmsg = optarg;
6240 795b88cb 2023-07-10 thomas break;
6241 795b88cb 2023-07-10 thomas case 'r':
6242 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
6243 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6244 795b88cb 2023-07-10 thomas error = got_error_from_errno2("realpath",
6245 795b88cb 2023-07-10 thomas optarg);
6246 795b88cb 2023-07-10 thomas goto done;
6247 795b88cb 2023-07-10 thomas }
6248 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
6249 795b88cb 2023-07-10 thomas break;
6250 795b88cb 2023-07-10 thomas case 's':
6251 795b88cb 2023-07-10 thomas signer_id = optarg;
6252 795b88cb 2023-07-10 thomas break;
6253 795b88cb 2023-07-10 thomas case 'V':
6254 795b88cb 2023-07-10 thomas verify_tags = 1;
6255 795b88cb 2023-07-10 thomas break;
6256 795b88cb 2023-07-10 thomas case 'v':
6257 795b88cb 2023-07-10 thomas if (verbosity < 0)
6258 795b88cb 2023-07-10 thomas verbosity = 0;
6259 795b88cb 2023-07-10 thomas else if (verbosity < 3)
6260 795b88cb 2023-07-10 thomas verbosity++;
6261 795b88cb 2023-07-10 thomas break;
6262 795b88cb 2023-07-10 thomas default:
6263 795b88cb 2023-07-10 thomas usage_tag();
6264 795b88cb 2023-07-10 thomas /* NOTREACHED */
6265 795b88cb 2023-07-10 thomas }
6266 795b88cb 2023-07-10 thomas }
6267 795b88cb 2023-07-10 thomas
6268 795b88cb 2023-07-10 thomas argc -= optind;
6269 795b88cb 2023-07-10 thomas argv += optind;
6270 795b88cb 2023-07-10 thomas
6271 795b88cb 2023-07-10 thomas if (do_list || verify_tags) {
6272 795b88cb 2023-07-10 thomas if (commit_id_arg != NULL)
6273 795b88cb 2023-07-10 thomas errx(1,
6274 795b88cb 2023-07-10 thomas "-c option can only be used when creating a tag");
6275 795b88cb 2023-07-10 thomas if (tagmsg) {
6276 795b88cb 2023-07-10 thomas if (do_list)
6277 795b88cb 2023-07-10 thomas option_conflict('l', 'm');
6278 795b88cb 2023-07-10 thomas else
6279 795b88cb 2023-07-10 thomas option_conflict('V', 'm');
6280 795b88cb 2023-07-10 thomas }
6281 795b88cb 2023-07-10 thomas if (signer_id) {
6282 795b88cb 2023-07-10 thomas if (do_list)
6283 795b88cb 2023-07-10 thomas option_conflict('l', 's');
6284 795b88cb 2023-07-10 thomas else
6285 795b88cb 2023-07-10 thomas option_conflict('V', 's');
6286 795b88cb 2023-07-10 thomas }
6287 795b88cb 2023-07-10 thomas if (argc > 1)
6288 795b88cb 2023-07-10 thomas usage_tag();
6289 795b88cb 2023-07-10 thomas } else if (argc != 1)
6290 795b88cb 2023-07-10 thomas usage_tag();
6291 795b88cb 2023-07-10 thomas
6292 795b88cb 2023-07-10 thomas if (argc == 1)
6293 795b88cb 2023-07-10 thomas tag_name = argv[0];
6294 795b88cb 2023-07-10 thomas
6295 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6296 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6297 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6298 795b88cb 2023-07-10 thomas goto done;
6299 795b88cb 2023-07-10 thomas }
6300 795b88cb 2023-07-10 thomas
6301 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6302 795b88cb 2023-07-10 thomas if (error != NULL)
6303 795b88cb 2023-07-10 thomas goto done;
6304 795b88cb 2023-07-10 thomas
6305 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6306 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6307 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
6308 795b88cb 2023-07-10 thomas goto done;
6309 795b88cb 2023-07-10 thomas else
6310 795b88cb 2023-07-10 thomas error = NULL;
6311 795b88cb 2023-07-10 thomas if (worktree) {
6312 795b88cb 2023-07-10 thomas repo_path =
6313 795b88cb 2023-07-10 thomas strdup(got_worktree_get_repo_path(worktree));
6314 795b88cb 2023-07-10 thomas if (repo_path == NULL)
6315 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
6316 795b88cb 2023-07-10 thomas if (error)
6317 795b88cb 2023-07-10 thomas goto done;
6318 795b88cb 2023-07-10 thomas } else {
6319 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
6320 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
6321 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
6322 795b88cb 2023-07-10 thomas goto done;
6323 795b88cb 2023-07-10 thomas }
6324 795b88cb 2023-07-10 thomas }
6325 795b88cb 2023-07-10 thomas }
6326 795b88cb 2023-07-10 thomas
6327 795b88cb 2023-07-10 thomas if (do_list || verify_tags) {
6328 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6329 795b88cb 2023-07-10 thomas if (error != NULL)
6330 795b88cb 2023-07-10 thomas goto done;
6331 795b88cb 2023-07-10 thomas error = get_allowed_signers(&allowed_signers, repo, worktree);
6332 795b88cb 2023-07-10 thomas if (error)
6333 795b88cb 2023-07-10 thomas goto done;
6334 795b88cb 2023-07-10 thomas error = get_revoked_signers(&revoked_signers, repo, worktree);
6335 795b88cb 2023-07-10 thomas if (error)
6336 795b88cb 2023-07-10 thomas goto done;
6337 795b88cb 2023-07-10 thomas if (worktree) {
6338 795b88cb 2023-07-10 thomas /* Release work tree lock. */
6339 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6340 795b88cb 2023-07-10 thomas worktree = NULL;
6341 795b88cb 2023-07-10 thomas }
6342 795b88cb 2023-07-10 thomas
6343 795b88cb 2023-07-10 thomas /*
6344 795b88cb 2023-07-10 thomas * Remove "cpath" promise unless needed for signature tmpfile
6345 795b88cb 2023-07-10 thomas * creation.
6346 795b88cb 2023-07-10 thomas */
6347 795b88cb 2023-07-10 thomas if (verify_tags)
6348 795b88cb 2023-07-10 thomas got_sigs_apply_unveil();
6349 795b88cb 2023-07-10 thomas else {
6350 795b88cb 2023-07-10 thomas #ifndef PROFILE
6351 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath flock proc exec sendfd "
6352 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
6353 795b88cb 2023-07-10 thomas err(1, "pledge");
6354 795b88cb 2023-07-10 thomas #endif
6355 795b88cb 2023-07-10 thomas }
6356 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6357 795b88cb 2023-07-10 thomas if (error)
6358 795b88cb 2023-07-10 thomas goto done;
6359 795b88cb 2023-07-10 thomas error = list_tags(repo, tag_name, verify_tags, allowed_signers,
6360 795b88cb 2023-07-10 thomas revoked_signers, verbosity);
6361 795b88cb 2023-07-10 thomas } else {
6362 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
6363 795b88cb 2023-07-10 thomas if (error)
6364 795b88cb 2023-07-10 thomas goto done;
6365 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, gitconfig_path,
6366 795b88cb 2023-07-10 thomas pack_fds);
6367 795b88cb 2023-07-10 thomas if (error != NULL)
6368 795b88cb 2023-07-10 thomas goto done;
6369 795b88cb 2023-07-10 thomas
6370 795b88cb 2023-07-10 thomas error = get_author(&tagger, repo, worktree);
6371 795b88cb 2023-07-10 thomas if (error)
6372 795b88cb 2023-07-10 thomas goto done;
6373 795b88cb 2023-07-10 thomas if (signer_id == NULL)
6374 795b88cb 2023-07-10 thomas signer_id = get_signer_id(repo, worktree);
6375 795b88cb 2023-07-10 thomas
6376 795b88cb 2023-07-10 thomas if (tagmsg) {
6377 795b88cb 2023-07-10 thomas if (signer_id) {
6378 795b88cb 2023-07-10 thomas error = got_sigs_apply_unveil();
6379 795b88cb 2023-07-10 thomas if (error)
6380 795b88cb 2023-07-10 thomas goto done;
6381 795b88cb 2023-07-10 thomas }
6382 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0, NULL);
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
6387 795b88cb 2023-07-10 thomas if (commit_id_arg == NULL) {
6388 795b88cb 2023-07-10 thomas struct got_reference *head_ref;
6389 795b88cb 2023-07-10 thomas struct got_object_id *commit_id;
6390 795b88cb 2023-07-10 thomas error = got_ref_open(&head_ref, repo,
6391 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_head_ref_name(worktree)
6392 795b88cb 2023-07-10 thomas : GOT_REF_HEAD, 0);
6393 795b88cb 2023-07-10 thomas if (error)
6394 795b88cb 2023-07-10 thomas goto done;
6395 795b88cb 2023-07-10 thomas error = got_ref_resolve(&commit_id, repo, head_ref);
6396 795b88cb 2023-07-10 thomas got_ref_close(head_ref);
6397 795b88cb 2023-07-10 thomas if (error)
6398 795b88cb 2023-07-10 thomas goto done;
6399 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
6400 795b88cb 2023-07-10 thomas free(commit_id);
6401 795b88cb 2023-07-10 thomas if (error)
6402 795b88cb 2023-07-10 thomas goto done;
6403 795b88cb 2023-07-10 thomas }
6404 795b88cb 2023-07-10 thomas
6405 795b88cb 2023-07-10 thomas if (worktree) {
6406 795b88cb 2023-07-10 thomas /* Release work tree lock. */
6407 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6408 795b88cb 2023-07-10 thomas worktree = NULL;
6409 795b88cb 2023-07-10 thomas }
6410 795b88cb 2023-07-10 thomas
6411 795b88cb 2023-07-10 thomas error = add_tag(repo, tagger, tag_name,
6412 795b88cb 2023-07-10 thomas commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
6413 795b88cb 2023-07-10 thomas signer_id, verbosity);
6414 795b88cb 2023-07-10 thomas }
6415 795b88cb 2023-07-10 thomas done:
6416 795b88cb 2023-07-10 thomas if (repo) {
6417 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6418 795b88cb 2023-07-10 thomas if (error == NULL)
6419 795b88cb 2023-07-10 thomas error = close_err;
6420 795b88cb 2023-07-10 thomas }
6421 795b88cb 2023-07-10 thomas if (worktree)
6422 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6423 795b88cb 2023-07-10 thomas if (pack_fds) {
6424 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6425 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6426 795b88cb 2023-07-10 thomas if (error == NULL)
6427 795b88cb 2023-07-10 thomas error = pack_err;
6428 795b88cb 2023-07-10 thomas }
6429 795b88cb 2023-07-10 thomas free(cwd);
6430 795b88cb 2023-07-10 thomas free(repo_path);
6431 795b88cb 2023-07-10 thomas free(gitconfig_path);
6432 795b88cb 2023-07-10 thomas free(commit_id_str);
6433 795b88cb 2023-07-10 thomas free(tagger);
6434 795b88cb 2023-07-10 thomas free(allowed_signers);
6435 795b88cb 2023-07-10 thomas free(revoked_signers);
6436 795b88cb 2023-07-10 thomas return error;
6437 795b88cb 2023-07-10 thomas }
6438 795b88cb 2023-07-10 thomas
6439 795b88cb 2023-07-10 thomas __dead static void
6440 795b88cb 2023-07-10 thomas usage_add(void)
6441 795b88cb 2023-07-10 thomas {
6442 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
6443 795b88cb 2023-07-10 thomas exit(1);
6444 795b88cb 2023-07-10 thomas }
6445 795b88cb 2023-07-10 thomas
6446 795b88cb 2023-07-10 thomas static const struct got_error *
6447 795b88cb 2023-07-10 thomas add_progress(void *arg, unsigned char status, const char *path)
6448 795b88cb 2023-07-10 thomas {
6449 795b88cb 2023-07-10 thomas while (path[0] == '/')
6450 795b88cb 2023-07-10 thomas path++;
6451 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
6452 795b88cb 2023-07-10 thomas return NULL;
6453 795b88cb 2023-07-10 thomas }
6454 795b88cb 2023-07-10 thomas
6455 795b88cb 2023-07-10 thomas static const struct got_error *
6456 795b88cb 2023-07-10 thomas cmd_add(int argc, char *argv[])
6457 795b88cb 2023-07-10 thomas {
6458 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6459 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6460 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6461 795b88cb 2023-07-10 thomas char *cwd = NULL;
6462 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
6463 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
6464 795b88cb 2023-07-10 thomas int ch, can_recurse = 0, no_ignores = 0;
6465 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6466 795b88cb 2023-07-10 thomas
6467 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
6468 795b88cb 2023-07-10 thomas
6469 795b88cb 2023-07-10 thomas #ifndef PROFILE
6470 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6471 795b88cb 2023-07-10 thomas NULL) == -1)
6472 795b88cb 2023-07-10 thomas err(1, "pledge");
6473 795b88cb 2023-07-10 thomas #endif
6474 795b88cb 2023-07-10 thomas
6475 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "IR")) != -1) {
6476 795b88cb 2023-07-10 thomas switch (ch) {
6477 795b88cb 2023-07-10 thomas case 'I':
6478 795b88cb 2023-07-10 thomas no_ignores = 1;
6479 795b88cb 2023-07-10 thomas break;
6480 795b88cb 2023-07-10 thomas case 'R':
6481 795b88cb 2023-07-10 thomas can_recurse = 1;
6482 795b88cb 2023-07-10 thomas break;
6483 795b88cb 2023-07-10 thomas default:
6484 795b88cb 2023-07-10 thomas usage_add();
6485 795b88cb 2023-07-10 thomas /* NOTREACHED */
6486 795b88cb 2023-07-10 thomas }
6487 795b88cb 2023-07-10 thomas }
6488 795b88cb 2023-07-10 thomas
6489 795b88cb 2023-07-10 thomas argc -= optind;
6490 795b88cb 2023-07-10 thomas argv += optind;
6491 795b88cb 2023-07-10 thomas
6492 795b88cb 2023-07-10 thomas if (argc < 1)
6493 795b88cb 2023-07-10 thomas usage_add();
6494 795b88cb 2023-07-10 thomas
6495 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6496 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6497 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6498 795b88cb 2023-07-10 thomas goto done;
6499 795b88cb 2023-07-10 thomas }
6500 795b88cb 2023-07-10 thomas
6501 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6502 795b88cb 2023-07-10 thomas if (error != NULL)
6503 795b88cb 2023-07-10 thomas goto done;
6504 795b88cb 2023-07-10 thomas
6505 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6506 795b88cb 2023-07-10 thomas if (error) {
6507 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6508 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "add", cwd);
6509 795b88cb 2023-07-10 thomas goto done;
6510 795b88cb 2023-07-10 thomas }
6511 795b88cb 2023-07-10 thomas
6512 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6513 795b88cb 2023-07-10 thomas NULL, pack_fds);
6514 795b88cb 2023-07-10 thomas if (error != NULL)
6515 795b88cb 2023-07-10 thomas goto done;
6516 795b88cb 2023-07-10 thomas
6517 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6518 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6519 795b88cb 2023-07-10 thomas if (error)
6520 795b88cb 2023-07-10 thomas goto done;
6521 795b88cb 2023-07-10 thomas
6522 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, 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 if (!can_recurse) {
6527 795b88cb 2023-07-10 thomas char *ondisk_path;
6528 795b88cb 2023-07-10 thomas struct stat sb;
6529 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
6530 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
6531 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
6532 795b88cb 2023-07-10 thomas pe->path) == -1) {
6533 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
6534 795b88cb 2023-07-10 thomas goto done;
6535 795b88cb 2023-07-10 thomas }
6536 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
6537 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
6538 795b88cb 2023-07-10 thomas free(ondisk_path);
6539 795b88cb 2023-07-10 thomas continue;
6540 795b88cb 2023-07-10 thomas }
6541 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
6542 795b88cb 2023-07-10 thomas ondisk_path);
6543 795b88cb 2023-07-10 thomas free(ondisk_path);
6544 795b88cb 2023-07-10 thomas goto done;
6545 795b88cb 2023-07-10 thomas }
6546 795b88cb 2023-07-10 thomas free(ondisk_path);
6547 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
6548 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
6549 795b88cb 2023-07-10 thomas "adding directories requires -R option");
6550 795b88cb 2023-07-10 thomas goto done;
6551 795b88cb 2023-07-10 thomas }
6552 795b88cb 2023-07-10 thomas }
6553 795b88cb 2023-07-10 thomas }
6554 795b88cb 2023-07-10 thomas
6555 795b88cb 2023-07-10 thomas error = got_worktree_schedule_add(worktree, &paths, add_progress,
6556 795b88cb 2023-07-10 thomas NULL, repo, no_ignores);
6557 795b88cb 2023-07-10 thomas done:
6558 795b88cb 2023-07-10 thomas if (repo) {
6559 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6560 795b88cb 2023-07-10 thomas if (error == NULL)
6561 795b88cb 2023-07-10 thomas error = close_err;
6562 795b88cb 2023-07-10 thomas }
6563 795b88cb 2023-07-10 thomas if (worktree)
6564 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6565 795b88cb 2023-07-10 thomas if (pack_fds) {
6566 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6567 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6568 795b88cb 2023-07-10 thomas if (error == NULL)
6569 795b88cb 2023-07-10 thomas error = pack_err;
6570 795b88cb 2023-07-10 thomas }
6571 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6572 795b88cb 2023-07-10 thomas free(cwd);
6573 795b88cb 2023-07-10 thomas return error;
6574 795b88cb 2023-07-10 thomas }
6575 795b88cb 2023-07-10 thomas
6576 795b88cb 2023-07-10 thomas __dead static void
6577 795b88cb 2023-07-10 thomas usage_remove(void)
6578 795b88cb 2023-07-10 thomas {
6579 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
6580 795b88cb 2023-07-10 thomas getprogname());
6581 795b88cb 2023-07-10 thomas exit(1);
6582 795b88cb 2023-07-10 thomas }
6583 795b88cb 2023-07-10 thomas
6584 795b88cb 2023-07-10 thomas static const struct got_error *
6585 795b88cb 2023-07-10 thomas print_remove_status(void *arg, unsigned char status,
6586 795b88cb 2023-07-10 thomas unsigned char staged_status, const char *path)
6587 795b88cb 2023-07-10 thomas {
6588 795b88cb 2023-07-10 thomas while (path[0] == '/')
6589 795b88cb 2023-07-10 thomas path++;
6590 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_NONEXISTENT)
6591 795b88cb 2023-07-10 thomas return NULL;
6592 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
6593 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
6594 795b88cb 2023-07-10 thomas printf("%c%c %s\n", status, staged_status, path);
6595 795b88cb 2023-07-10 thomas return NULL;
6596 795b88cb 2023-07-10 thomas }
6597 795b88cb 2023-07-10 thomas
6598 795b88cb 2023-07-10 thomas static const struct got_error *
6599 795b88cb 2023-07-10 thomas cmd_remove(int argc, char *argv[])
6600 795b88cb 2023-07-10 thomas {
6601 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
6602 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6603 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6604 795b88cb 2023-07-10 thomas const char *status_codes = NULL;
6605 795b88cb 2023-07-10 thomas char *cwd = NULL;
6606 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
6607 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
6608 795b88cb 2023-07-10 thomas int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6609 795b88cb 2023-07-10 thomas int ignore_missing_paths = 0;
6610 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6611 795b88cb 2023-07-10 thomas
6612 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
6613 795b88cb 2023-07-10 thomas
6614 795b88cb 2023-07-10 thomas #ifndef PROFILE
6615 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6616 795b88cb 2023-07-10 thomas NULL) == -1)
6617 795b88cb 2023-07-10 thomas err(1, "pledge");
6618 795b88cb 2023-07-10 thomas #endif
6619 795b88cb 2023-07-10 thomas
6620 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6621 795b88cb 2023-07-10 thomas switch (ch) {
6622 795b88cb 2023-07-10 thomas case 'f':
6623 795b88cb 2023-07-10 thomas delete_local_mods = 1;
6624 795b88cb 2023-07-10 thomas ignore_missing_paths = 1;
6625 795b88cb 2023-07-10 thomas break;
6626 795b88cb 2023-07-10 thomas case 'k':
6627 795b88cb 2023-07-10 thomas keep_on_disk = 1;
6628 795b88cb 2023-07-10 thomas break;
6629 795b88cb 2023-07-10 thomas case 'R':
6630 795b88cb 2023-07-10 thomas can_recurse = 1;
6631 795b88cb 2023-07-10 thomas break;
6632 795b88cb 2023-07-10 thomas case 's':
6633 795b88cb 2023-07-10 thomas for (i = 0; optarg[i] != '\0'; i++) {
6634 795b88cb 2023-07-10 thomas switch (optarg[i]) {
6635 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
6636 795b88cb 2023-07-10 thomas delete_local_mods = 1;
6637 795b88cb 2023-07-10 thomas break;
6638 795b88cb 2023-07-10 thomas case GOT_STATUS_MISSING:
6639 795b88cb 2023-07-10 thomas ignore_missing_paths = 1;
6640 795b88cb 2023-07-10 thomas break;
6641 795b88cb 2023-07-10 thomas default:
6642 795b88cb 2023-07-10 thomas errx(1, "invalid status code '%c'",
6643 795b88cb 2023-07-10 thomas optarg[i]);
6644 795b88cb 2023-07-10 thomas }
6645 795b88cb 2023-07-10 thomas }
6646 795b88cb 2023-07-10 thomas status_codes = optarg;
6647 795b88cb 2023-07-10 thomas break;
6648 795b88cb 2023-07-10 thomas default:
6649 795b88cb 2023-07-10 thomas usage_remove();
6650 795b88cb 2023-07-10 thomas /* NOTREACHED */
6651 795b88cb 2023-07-10 thomas }
6652 795b88cb 2023-07-10 thomas }
6653 795b88cb 2023-07-10 thomas
6654 795b88cb 2023-07-10 thomas argc -= optind;
6655 795b88cb 2023-07-10 thomas argv += optind;
6656 795b88cb 2023-07-10 thomas
6657 795b88cb 2023-07-10 thomas if (argc < 1)
6658 795b88cb 2023-07-10 thomas usage_remove();
6659 795b88cb 2023-07-10 thomas
6660 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
6661 795b88cb 2023-07-10 thomas if (cwd == NULL) {
6662 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6663 795b88cb 2023-07-10 thomas goto done;
6664 795b88cb 2023-07-10 thomas }
6665 795b88cb 2023-07-10 thomas
6666 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6667 795b88cb 2023-07-10 thomas if (error != NULL)
6668 795b88cb 2023-07-10 thomas goto done;
6669 795b88cb 2023-07-10 thomas
6670 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6671 795b88cb 2023-07-10 thomas if (error) {
6672 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
6673 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "remove", cwd);
6674 795b88cb 2023-07-10 thomas goto done;
6675 795b88cb 2023-07-10 thomas }
6676 795b88cb 2023-07-10 thomas
6677 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6678 795b88cb 2023-07-10 thomas NULL, pack_fds);
6679 795b88cb 2023-07-10 thomas if (error)
6680 795b88cb 2023-07-10 thomas goto done;
6681 795b88cb 2023-07-10 thomas
6682 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1,
6683 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6684 795b88cb 2023-07-10 thomas if (error)
6685 795b88cb 2023-07-10 thomas goto done;
6686 795b88cb 2023-07-10 thomas
6687 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, 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 if (!can_recurse) {
6692 795b88cb 2023-07-10 thomas char *ondisk_path;
6693 795b88cb 2023-07-10 thomas struct stat sb;
6694 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
6695 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
6696 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
6697 795b88cb 2023-07-10 thomas pe->path) == -1) {
6698 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
6699 795b88cb 2023-07-10 thomas goto done;
6700 795b88cb 2023-07-10 thomas }
6701 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
6702 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
6703 795b88cb 2023-07-10 thomas free(ondisk_path);
6704 795b88cb 2023-07-10 thomas continue;
6705 795b88cb 2023-07-10 thomas }
6706 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
6707 795b88cb 2023-07-10 thomas ondisk_path);
6708 795b88cb 2023-07-10 thomas free(ondisk_path);
6709 795b88cb 2023-07-10 thomas goto done;
6710 795b88cb 2023-07-10 thomas }
6711 795b88cb 2023-07-10 thomas free(ondisk_path);
6712 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
6713 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
6714 795b88cb 2023-07-10 thomas "removing directories requires -R option");
6715 795b88cb 2023-07-10 thomas goto done;
6716 795b88cb 2023-07-10 thomas }
6717 795b88cb 2023-07-10 thomas }
6718 795b88cb 2023-07-10 thomas }
6719 795b88cb 2023-07-10 thomas
6720 795b88cb 2023-07-10 thomas error = got_worktree_schedule_delete(worktree, &paths,
6721 795b88cb 2023-07-10 thomas delete_local_mods, status_codes, print_remove_status, NULL,
6722 795b88cb 2023-07-10 thomas repo, keep_on_disk, ignore_missing_paths);
6723 795b88cb 2023-07-10 thomas done:
6724 795b88cb 2023-07-10 thomas if (repo) {
6725 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
6726 795b88cb 2023-07-10 thomas if (error == NULL)
6727 795b88cb 2023-07-10 thomas error = close_err;
6728 795b88cb 2023-07-10 thomas }
6729 795b88cb 2023-07-10 thomas if (worktree)
6730 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
6731 795b88cb 2023-07-10 thomas if (pack_fds) {
6732 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6733 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6734 795b88cb 2023-07-10 thomas if (error == NULL)
6735 795b88cb 2023-07-10 thomas error = pack_err;
6736 795b88cb 2023-07-10 thomas }
6737 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6738 795b88cb 2023-07-10 thomas free(cwd);
6739 795b88cb 2023-07-10 thomas return error;
6740 795b88cb 2023-07-10 thomas }
6741 795b88cb 2023-07-10 thomas
6742 795b88cb 2023-07-10 thomas __dead static void
6743 795b88cb 2023-07-10 thomas usage_patch(void)
6744 795b88cb 2023-07-10 thomas {
6745 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
6746 795b88cb 2023-07-10 thomas "[patchfile]\n", getprogname());
6747 795b88cb 2023-07-10 thomas exit(1);
6748 795b88cb 2023-07-10 thomas }
6749 795b88cb 2023-07-10 thomas
6750 795b88cb 2023-07-10 thomas static const struct got_error *
6751 795b88cb 2023-07-10 thomas patch_from_stdin(int *patchfd)
6752 795b88cb 2023-07-10 thomas {
6753 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
6754 795b88cb 2023-07-10 thomas ssize_t r;
6755 795b88cb 2023-07-10 thomas char buf[BUFSIZ];
6756 795b88cb 2023-07-10 thomas sig_t sighup, sigint, sigquit;
6757 795b88cb 2023-07-10 thomas
6758 795b88cb 2023-07-10 thomas *patchfd = got_opentempfd();
6759 795b88cb 2023-07-10 thomas if (*patchfd == -1)
6760 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
6761 795b88cb 2023-07-10 thomas
6762 795b88cb 2023-07-10 thomas sighup = signal(SIGHUP, SIG_DFL);
6763 795b88cb 2023-07-10 thomas sigint = signal(SIGINT, SIG_DFL);
6764 795b88cb 2023-07-10 thomas sigquit = signal(SIGQUIT, SIG_DFL);
6765 795b88cb 2023-07-10 thomas
6766 795b88cb 2023-07-10 thomas for (;;) {
6767 795b88cb 2023-07-10 thomas r = read(0, buf, sizeof(buf));
6768 795b88cb 2023-07-10 thomas if (r == -1) {
6769 795b88cb 2023-07-10 thomas err = got_error_from_errno("read");
6770 795b88cb 2023-07-10 thomas break;
6771 795b88cb 2023-07-10 thomas }
6772 795b88cb 2023-07-10 thomas if (r == 0)
6773 795b88cb 2023-07-10 thomas break;
6774 795b88cb 2023-07-10 thomas if (write(*patchfd, buf, r) == -1) {
6775 795b88cb 2023-07-10 thomas err = got_error_from_errno("write");
6776 795b88cb 2023-07-10 thomas break;
6777 795b88cb 2023-07-10 thomas }
6778 795b88cb 2023-07-10 thomas }
6779 795b88cb 2023-07-10 thomas
6780 795b88cb 2023-07-10 thomas signal(SIGHUP, sighup);
6781 795b88cb 2023-07-10 thomas signal(SIGINT, sigint);
6782 795b88cb 2023-07-10 thomas signal(SIGQUIT, sigquit);
6783 795b88cb 2023-07-10 thomas
6784 795b88cb 2023-07-10 thomas if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
6785 795b88cb 2023-07-10 thomas err = got_error_from_errno("lseek");
6786 795b88cb 2023-07-10 thomas
6787 795b88cb 2023-07-10 thomas if (err != NULL) {
6788 795b88cb 2023-07-10 thomas close(*patchfd);
6789 795b88cb 2023-07-10 thomas *patchfd = -1;
6790 795b88cb 2023-07-10 thomas }
6791 795b88cb 2023-07-10 thomas
6792 795b88cb 2023-07-10 thomas return err;
6793 795b88cb 2023-07-10 thomas }
6794 795b88cb 2023-07-10 thomas
6795 795b88cb 2023-07-10 thomas struct got_patch_progress_arg {
6796 795b88cb 2023-07-10 thomas int did_something;
6797 795b88cb 2023-07-10 thomas int conflicts;
6798 795b88cb 2023-07-10 thomas int rejects;
6799 795b88cb 2023-07-10 thomas };
6800 795b88cb 2023-07-10 thomas
6801 795b88cb 2023-07-10 thomas static const struct got_error *
6802 795b88cb 2023-07-10 thomas patch_progress(void *arg, const char *old, const char *new,
6803 795b88cb 2023-07-10 thomas unsigned char status, const struct got_error *error, int old_from,
6804 795b88cb 2023-07-10 thomas int old_lines, int new_from, int new_lines, int offset,
6805 795b88cb 2023-07-10 thomas int ws_mangled, const struct got_error *hunk_err)
6806 795b88cb 2023-07-10 thomas {
6807 795b88cb 2023-07-10 thomas const char *path = new == NULL ? old : new;
6808 795b88cb 2023-07-10 thomas struct got_patch_progress_arg *a = arg;
6809 795b88cb 2023-07-10 thomas
6810 795b88cb 2023-07-10 thomas while (*path == '/')
6811 795b88cb 2023-07-10 thomas path++;
6812 795b88cb 2023-07-10 thomas
6813 795b88cb 2023-07-10 thomas if (status != GOT_STATUS_NO_CHANGE &&
6814 795b88cb 2023-07-10 thomas status != 0 /* per-hunk progress */) {
6815 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
6816 795b88cb 2023-07-10 thomas a->did_something = 1;
6817 795b88cb 2023-07-10 thomas }
6818 795b88cb 2023-07-10 thomas
6819 795b88cb 2023-07-10 thomas if (hunk_err == NULL) {
6820 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_CANNOT_UPDATE)
6821 795b88cb 2023-07-10 thomas a->rejects++;
6822 795b88cb 2023-07-10 thomas else if (status == GOT_STATUS_CONFLICT)
6823 795b88cb 2023-07-10 thomas a->conflicts++;
6824 795b88cb 2023-07-10 thomas }
6825 795b88cb 2023-07-10 thomas
6826 795b88cb 2023-07-10 thomas if (error != NULL)
6827 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6828 795b88cb 2023-07-10 thomas
6829 795b88cb 2023-07-10 thomas if (offset != 0 || hunk_err != NULL || ws_mangled) {
6830 795b88cb 2023-07-10 thomas printf("@@ -%d,%d +%d,%d @@ ", old_from,
6831 795b88cb 2023-07-10 thomas old_lines, new_from, new_lines);
6832 795b88cb 2023-07-10 thomas if (hunk_err != NULL)
6833 795b88cb 2023-07-10 thomas printf("%s\n", hunk_err->msg);
6834 795b88cb 2023-07-10 thomas else if (offset != 0)
6835 795b88cb 2023-07-10 thomas printf("applied with offset %d\n", offset);
6836 795b88cb 2023-07-10 thomas else
6837 795b88cb 2023-07-10 thomas printf("hunk contains mangled whitespace\n");
6838 795b88cb 2023-07-10 thomas }
6839 795b88cb 2023-07-10 thomas
6840 795b88cb 2023-07-10 thomas return NULL;
6841 795b88cb 2023-07-10 thomas }
6842 795b88cb 2023-07-10 thomas
6843 795b88cb 2023-07-10 thomas static void
6844 795b88cb 2023-07-10 thomas print_patch_progress_stats(struct got_patch_progress_arg *ppa)
6845 795b88cb 2023-07-10 thomas {
6846 795b88cb 2023-07-10 thomas if (!ppa->did_something)
6847 795b88cb 2023-07-10 thomas return;
6848 795b88cb 2023-07-10 thomas
6849 795b88cb 2023-07-10 thomas if (ppa->conflicts > 0)
6850 795b88cb 2023-07-10 thomas printf("Files with merge conflicts: %d\n", ppa->conflicts);
6851 795b88cb 2023-07-10 thomas
6852 795b88cb 2023-07-10 thomas if (ppa->rejects > 0) {
6853 795b88cb 2023-07-10 thomas printf("Files where patch failed to apply: %d\n",
6854 795b88cb 2023-07-10 thomas ppa->rejects);
6855 795b88cb 2023-07-10 thomas }
6856 795b88cb 2023-07-10 thomas }
6857 795b88cb 2023-07-10 thomas
6858 795b88cb 2023-07-10 thomas static const struct got_error *
6859 795b88cb 2023-07-10 thomas cmd_patch(int argc, char *argv[])
6860 795b88cb 2023-07-10 thomas {
6861 795b88cb 2023-07-10 thomas const struct got_error *error = NULL, *close_error = NULL;
6862 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
6863 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
6864 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
6865 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
6866 795b88cb 2023-07-10 thomas const char *commit_id_str = NULL;
6867 795b88cb 2023-07-10 thomas struct stat sb;
6868 795b88cb 2023-07-10 thomas const char *errstr;
6869 795b88cb 2023-07-10 thomas char *cwd = NULL;
6870 795b88cb 2023-07-10 thomas int ch, nop = 0, strip = -1, reverse = 0;
6871 795b88cb 2023-07-10 thomas int patchfd;
6872 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
6873 795b88cb 2023-07-10 thomas struct got_patch_progress_arg ppa;
6874 795b88cb 2023-07-10 thomas
6875 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
6876 795b88cb 2023-07-10 thomas
6877 795b88cb 2023-07-10 thomas #ifndef PROFILE
6878 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
6879 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
6880 795b88cb 2023-07-10 thomas err(1, "pledge");
6881 795b88cb 2023-07-10 thomas #endif
6882 795b88cb 2023-07-10 thomas
6883 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
6884 795b88cb 2023-07-10 thomas switch (ch) {
6885 795b88cb 2023-07-10 thomas case 'c':
6886 795b88cb 2023-07-10 thomas commit_id_str = optarg;
6887 795b88cb 2023-07-10 thomas break;
6888 795b88cb 2023-07-10 thomas case 'n':
6889 795b88cb 2023-07-10 thomas nop = 1;
6890 795b88cb 2023-07-10 thomas break;
6891 795b88cb 2023-07-10 thomas case 'p':
6892 795b88cb 2023-07-10 thomas strip = strtonum(optarg, 0, INT_MAX, &errstr);
6893 795b88cb 2023-07-10 thomas if (errstr != NULL)
6894 795b88cb 2023-07-10 thomas errx(1, "pathname strip count is %s: %s",
6895 795b88cb 2023-07-10 thomas errstr, optarg);
6896 795b88cb 2023-07-10 thomas break;
6897 795b88cb 2023-07-10 thomas case 'R':
6898 795b88cb 2023-07-10 thomas reverse = 1;
6899 795b88cb 2023-07-10 thomas break;
6900 795b88cb 2023-07-10 thomas default:
6901 795b88cb 2023-07-10 thomas usage_patch();
6902 795b88cb 2023-07-10 thomas /* NOTREACHED */
6903 795b88cb 2023-07-10 thomas }
6904 795b88cb 2023-07-10 thomas }
6905 795b88cb 2023-07-10 thomas
6906 795b88cb 2023-07-10 thomas argc -= optind;
6907 795b88cb 2023-07-10 thomas argv += optind;
6908 795b88cb 2023-07-10 thomas
6909 795b88cb 2023-07-10 thomas if (argc == 0) {
6910 795b88cb 2023-07-10 thomas error = patch_from_stdin(&patchfd);
6911 795b88cb 2023-07-10 thomas if (error)
6912 795b88cb 2023-07-10 thomas return error;
6913 795b88cb 2023-07-10 thomas } else if (argc == 1) {
6914 795b88cb 2023-07-10 thomas patchfd = open(argv[0], O_RDONLY);
6915 795b88cb 2023-07-10 thomas if (patchfd == -1) {
6916 795b88cb 2023-07-10 thomas error = got_error_from_errno2("open", argv[0]);
6917 795b88cb 2023-07-10 thomas return error;
6918 795b88cb 2023-07-10 thomas }
6919 795b88cb 2023-07-10 thomas if (fstat(patchfd, &sb) == -1) {
6920 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fstat", argv[0]);
6921 795b88cb 2023-07-10 thomas goto done;
6922 795b88cb 2023-07-10 thomas }
6923 795b88cb 2023-07-10 thomas if (!S_ISREG(sb.st_mode)) {
6924 795b88cb 2023-07-10 thomas error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
6925 795b88cb 2023-07-10 thomas goto done;
6926 795b88cb 2023-07-10 thomas }
6927 795b88cb 2023-07-10 thomas } else
6928 795b88cb 2023-07-10 thomas usage_patch();
6929 795b88cb 2023-07-10 thomas
6930 795b88cb 2023-07-10 thomas if ((cwd = getcwd(NULL, 0)) == NULL) {
6931 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
6932 795b88cb 2023-07-10 thomas goto done;
6933 795b88cb 2023-07-10 thomas }
6934 795b88cb 2023-07-10 thomas
6935 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
6936 795b88cb 2023-07-10 thomas if (error != NULL)
6937 795b88cb 2023-07-10 thomas goto done;
6938 795b88cb 2023-07-10 thomas
6939 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
6940 795b88cb 2023-07-10 thomas if (error != NULL)
6941 795b88cb 2023-07-10 thomas goto done;
6942 795b88cb 2023-07-10 thomas
6943 795b88cb 2023-07-10 thomas const char *repo_path = got_worktree_get_repo_path(worktree);
6944 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6945 795b88cb 2023-07-10 thomas if (error != NULL)
6946 795b88cb 2023-07-10 thomas goto done;
6947 795b88cb 2023-07-10 thomas
6948 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
6949 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
6950 795b88cb 2023-07-10 thomas if (error != NULL)
6951 795b88cb 2023-07-10 thomas goto done;
6952 795b88cb 2023-07-10 thomas
6953 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6954 795b88cb 2023-07-10 thomas if (error)
6955 795b88cb 2023-07-10 thomas goto done;
6956 795b88cb 2023-07-10 thomas
6957 795b88cb 2023-07-10 thomas if (commit_id_str != NULL) {
6958 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
6959 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6960 795b88cb 2023-07-10 thomas if (error)
6961 795b88cb 2023-07-10 thomas goto done;
6962 795b88cb 2023-07-10 thomas }
6963 795b88cb 2023-07-10 thomas
6964 795b88cb 2023-07-10 thomas memset(&ppa, 0, sizeof(ppa));
6965 795b88cb 2023-07-10 thomas error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
6966 795b88cb 2023-07-10 thomas commit_id, patch_progress, &ppa, check_cancelled, NULL);
6967 795b88cb 2023-07-10 thomas print_patch_progress_stats(&ppa);
6968 795b88cb 2023-07-10 thomas done:
6969 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
6970 795b88cb 2023-07-10 thomas free(commit_id);
6971 795b88cb 2023-07-10 thomas if (repo) {
6972 795b88cb 2023-07-10 thomas close_error = got_repo_close(repo);
6973 795b88cb 2023-07-10 thomas if (error == NULL)
6974 795b88cb 2023-07-10 thomas error = close_error;
6975 795b88cb 2023-07-10 thomas }
6976 795b88cb 2023-07-10 thomas if (worktree != NULL) {
6977 795b88cb 2023-07-10 thomas close_error = got_worktree_close(worktree);
6978 795b88cb 2023-07-10 thomas if (error == NULL)
6979 795b88cb 2023-07-10 thomas error = close_error;
6980 795b88cb 2023-07-10 thomas }
6981 795b88cb 2023-07-10 thomas if (pack_fds) {
6982 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
6983 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
6984 795b88cb 2023-07-10 thomas if (error == NULL)
6985 795b88cb 2023-07-10 thomas error = pack_err;
6986 795b88cb 2023-07-10 thomas }
6987 795b88cb 2023-07-10 thomas free(cwd);
6988 795b88cb 2023-07-10 thomas return error;
6989 795b88cb 2023-07-10 thomas }
6990 795b88cb 2023-07-10 thomas
6991 795b88cb 2023-07-10 thomas __dead static void
6992 795b88cb 2023-07-10 thomas usage_revert(void)
6993 795b88cb 2023-07-10 thomas {
6994 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
6995 795b88cb 2023-07-10 thomas getprogname());
6996 795b88cb 2023-07-10 thomas exit(1);
6997 795b88cb 2023-07-10 thomas }
6998 795b88cb 2023-07-10 thomas
6999 795b88cb 2023-07-10 thomas static const struct got_error *
7000 795b88cb 2023-07-10 thomas revert_progress(void *arg, unsigned char status, const char *path)
7001 795b88cb 2023-07-10 thomas {
7002 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_UNVERSIONED)
7003 795b88cb 2023-07-10 thomas return NULL;
7004 795b88cb 2023-07-10 thomas
7005 795b88cb 2023-07-10 thomas while (path[0] == '/')
7006 795b88cb 2023-07-10 thomas path++;
7007 795b88cb 2023-07-10 thomas printf("%c %s\n", status, path);
7008 795b88cb 2023-07-10 thomas return NULL;
7009 795b88cb 2023-07-10 thomas }
7010 795b88cb 2023-07-10 thomas
7011 795b88cb 2023-07-10 thomas struct choose_patch_arg {
7012 795b88cb 2023-07-10 thomas FILE *patch_script_file;
7013 795b88cb 2023-07-10 thomas const char *action;
7014 795b88cb 2023-07-10 thomas };
7015 795b88cb 2023-07-10 thomas
7016 795b88cb 2023-07-10 thomas static const struct got_error *
7017 795b88cb 2023-07-10 thomas show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7018 795b88cb 2023-07-10 thomas int nchanges, const char *action)
7019 795b88cb 2023-07-10 thomas {
7020 795b88cb 2023-07-10 thomas const struct got_error *err;
7021 795b88cb 2023-07-10 thomas char *line = NULL;
7022 795b88cb 2023-07-10 thomas size_t linesize = 0;
7023 795b88cb 2023-07-10 thomas ssize_t linelen;
7024 795b88cb 2023-07-10 thomas
7025 795b88cb 2023-07-10 thomas switch (status) {
7026 795b88cb 2023-07-10 thomas case GOT_STATUS_ADD:
7027 795b88cb 2023-07-10 thomas printf("A %s\n%s this addition? [y/n] ", path, action);
7028 795b88cb 2023-07-10 thomas break;
7029 795b88cb 2023-07-10 thomas case GOT_STATUS_DELETE:
7030 795b88cb 2023-07-10 thomas printf("D %s\n%s this deletion? [y/n] ", path, action);
7031 795b88cb 2023-07-10 thomas break;
7032 795b88cb 2023-07-10 thomas case GOT_STATUS_MODIFY:
7033 795b88cb 2023-07-10 thomas if (fseek(patch_file, 0L, SEEK_SET) == -1)
7034 795b88cb 2023-07-10 thomas return got_error_from_errno("fseek");
7035 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
7036 795b88cb 2023-07-10 thomas while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7037 795b88cb 2023-07-10 thomas printf("%s", line);
7038 795b88cb 2023-07-10 thomas if (linelen == -1 && ferror(patch_file)) {
7039 795b88cb 2023-07-10 thomas err = got_error_from_errno("getline");
7040 795b88cb 2023-07-10 thomas free(line);
7041 795b88cb 2023-07-10 thomas return err;
7042 795b88cb 2023-07-10 thomas }
7043 795b88cb 2023-07-10 thomas free(line);
7044 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
7045 795b88cb 2023-07-10 thomas printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7046 795b88cb 2023-07-10 thomas path, n, nchanges, action);
7047 795b88cb 2023-07-10 thomas break;
7048 795b88cb 2023-07-10 thomas default:
7049 795b88cb 2023-07-10 thomas return got_error_path(path, GOT_ERR_FILE_STATUS);
7050 795b88cb 2023-07-10 thomas }
7051 795b88cb 2023-07-10 thomas
7052 795b88cb 2023-07-10 thomas fflush(stdout);
7053 795b88cb 2023-07-10 thomas return NULL;
7054 795b88cb 2023-07-10 thomas }
7055 795b88cb 2023-07-10 thomas
7056 795b88cb 2023-07-10 thomas static const struct got_error *
7057 795b88cb 2023-07-10 thomas choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7058 795b88cb 2023-07-10 thomas FILE *patch_file, int n, int nchanges)
7059 795b88cb 2023-07-10 thomas {
7060 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7061 795b88cb 2023-07-10 thomas char *line = NULL;
7062 795b88cb 2023-07-10 thomas size_t linesize = 0;
7063 795b88cb 2023-07-10 thomas ssize_t linelen;
7064 795b88cb 2023-07-10 thomas int resp = ' ';
7065 795b88cb 2023-07-10 thomas struct choose_patch_arg *a = arg;
7066 795b88cb 2023-07-10 thomas
7067 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NONE;
7068 795b88cb 2023-07-10 thomas
7069 795b88cb 2023-07-10 thomas if (a->patch_script_file) {
7070 795b88cb 2023-07-10 thomas char *nl;
7071 795b88cb 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
7072 795b88cb 2023-07-10 thomas a->action);
7073 795b88cb 2023-07-10 thomas if (err)
7074 795b88cb 2023-07-10 thomas return err;
7075 795b88cb 2023-07-10 thomas linelen = getline(&line, &linesize, a->patch_script_file);
7076 795b88cb 2023-07-10 thomas if (linelen == -1) {
7077 795b88cb 2023-07-10 thomas if (ferror(a->patch_script_file))
7078 795b88cb 2023-07-10 thomas return got_error_from_errno("getline");
7079 795b88cb 2023-07-10 thomas return NULL;
7080 795b88cb 2023-07-10 thomas }
7081 795b88cb 2023-07-10 thomas nl = strchr(line, '\n');
7082 795b88cb 2023-07-10 thomas if (nl)
7083 795b88cb 2023-07-10 thomas *nl = '\0';
7084 795b88cb 2023-07-10 thomas if (strcmp(line, "y") == 0) {
7085 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
7086 795b88cb 2023-07-10 thomas printf("y\n");
7087 795b88cb 2023-07-10 thomas } else if (strcmp(line, "n") == 0) {
7088 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
7089 795b88cb 2023-07-10 thomas printf("n\n");
7090 795b88cb 2023-07-10 thomas } else if (strcmp(line, "q") == 0 &&
7091 795b88cb 2023-07-10 thomas status == GOT_STATUS_MODIFY) {
7092 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
7093 795b88cb 2023-07-10 thomas printf("q\n");
7094 795b88cb 2023-07-10 thomas } else
7095 795b88cb 2023-07-10 thomas printf("invalid response '%s'\n", line);
7096 795b88cb 2023-07-10 thomas free(line);
7097 795b88cb 2023-07-10 thomas return NULL;
7098 795b88cb 2023-07-10 thomas }
7099 795b88cb 2023-07-10 thomas
7100 795b88cb 2023-07-10 thomas while (resp != 'y' && resp != 'n' && resp != 'q') {
7101 795b88cb 2023-07-10 thomas err = show_change(status, path, patch_file, n, nchanges,
7102 795b88cb 2023-07-10 thomas a->action);
7103 795b88cb 2023-07-10 thomas if (err)
7104 795b88cb 2023-07-10 thomas return err;
7105 795b88cb 2023-07-10 thomas resp = getchar();
7106 795b88cb 2023-07-10 thomas if (resp == '\n')
7107 795b88cb 2023-07-10 thomas resp = getchar();
7108 795b88cb 2023-07-10 thomas if (status == GOT_STATUS_MODIFY) {
7109 795b88cb 2023-07-10 thomas if (resp != 'y' && resp != 'n' && resp != 'q') {
7110 795b88cb 2023-07-10 thomas printf("invalid response '%c'\n", resp);
7111 795b88cb 2023-07-10 thomas resp = ' ';
7112 795b88cb 2023-07-10 thomas }
7113 795b88cb 2023-07-10 thomas } else if (resp != 'y' && resp != 'n') {
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 }
7118 795b88cb 2023-07-10 thomas
7119 795b88cb 2023-07-10 thomas if (resp == 'y')
7120 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_YES;
7121 795b88cb 2023-07-10 thomas else if (resp == 'n')
7122 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_NO;
7123 795b88cb 2023-07-10 thomas else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7124 795b88cb 2023-07-10 thomas *choice = GOT_PATCH_CHOICE_QUIT;
7125 795b88cb 2023-07-10 thomas
7126 795b88cb 2023-07-10 thomas return NULL;
7127 795b88cb 2023-07-10 thomas }
7128 795b88cb 2023-07-10 thomas
7129 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg {
7130 795b88cb 2023-07-10 thomas struct got_pathlist_head *commit_paths;
7131 795b88cb 2023-07-10 thomas int *has_changes;
7132 795b88cb 2023-07-10 thomas };
7133 795b88cb 2023-07-10 thomas
7134 795b88cb 2023-07-10 thomas /*
7135 795b88cb 2023-07-10 thomas * Shortcut work tree status callback to determine if the set of paths scanned
7136 795b88cb 2023-07-10 thomas * has at least one versioned path that is being modified and, if not NULL, is
7137 795b88cb 2023-07-10 thomas * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
7138 795b88cb 2023-07-10 thomas * soon as a path is passed with a status that satisfies this criteria.
7139 795b88cb 2023-07-10 thomas */
7140 795b88cb 2023-07-10 thomas static const struct got_error *
7141 795b88cb 2023-07-10 thomas worktree_has_commitable_path(void *arg, unsigned char status,
7142 795b88cb 2023-07-10 thomas unsigned char staged_status, const char *path,
7143 795b88cb 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7144 795b88cb 2023-07-10 thomas struct got_object_id *commit_id, int dirfd, const char *de_name)
7145 795b88cb 2023-07-10 thomas {
7146 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg *a = arg;
7147 795b88cb 2023-07-10 thomas
7148 795b88cb 2023-07-10 thomas if (status == staged_status && (status == GOT_STATUS_DELETE))
7149 795b88cb 2023-07-10 thomas status = GOT_STATUS_NO_CHANGE;
7150 795b88cb 2023-07-10 thomas
7151 795b88cb 2023-07-10 thomas if (!(status == GOT_STATUS_NO_CHANGE ||
7152 795b88cb 2023-07-10 thomas status == GOT_STATUS_UNVERSIONED) ||
7153 795b88cb 2023-07-10 thomas staged_status != GOT_STATUS_NO_CHANGE) {
7154 795b88cb 2023-07-10 thomas if (a->commit_paths != NULL) {
7155 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7156 795b88cb 2023-07-10 thomas
7157 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, a->commit_paths, entry) {
7158 795b88cb 2023-07-10 thomas if (strncmp(path, pe->path,
7159 795b88cb 2023-07-10 thomas pe->path_len) == 0) {
7160 795b88cb 2023-07-10 thomas *a->has_changes = 1;
7161 795b88cb 2023-07-10 thomas break;
7162 795b88cb 2023-07-10 thomas }
7163 795b88cb 2023-07-10 thomas }
7164 795b88cb 2023-07-10 thomas } else
7165 795b88cb 2023-07-10 thomas *a->has_changes = 1;
7166 795b88cb 2023-07-10 thomas
7167 795b88cb 2023-07-10 thomas if (*a->has_changes)
7168 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_FILE_MODIFIED);
7169 795b88cb 2023-07-10 thomas }
7170 795b88cb 2023-07-10 thomas
7171 795b88cb 2023-07-10 thomas return NULL;
7172 795b88cb 2023-07-10 thomas }
7173 795b88cb 2023-07-10 thomas
7174 795b88cb 2023-07-10 thomas /*
7175 795b88cb 2023-07-10 thomas * Check that the changeset of the commit identified by id is
7176 795b88cb 2023-07-10 thomas * comprised of at least one modified path that is being committed.
7177 795b88cb 2023-07-10 thomas */
7178 795b88cb 2023-07-10 thomas static const struct got_error *
7179 795b88cb 2023-07-10 thomas commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
7180 795b88cb 2023-07-10 thomas struct got_object_id *id, struct got_worktree *worktree,
7181 795b88cb 2023-07-10 thomas struct got_repository *repo)
7182 795b88cb 2023-07-10 thomas {
7183 795b88cb 2023-07-10 thomas const struct got_error *err;
7184 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7185 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL, *pcommit = NULL;
7186 795b88cb 2023-07-10 thomas struct got_tree_object *tree = NULL, *ptree = NULL;
7187 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
7188 795b88cb 2023-07-10 thomas
7189 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7190 795b88cb 2023-07-10 thomas
7191 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
7192 795b88cb 2023-07-10 thomas if (err)
7193 795b88cb 2023-07-10 thomas goto done;
7194 795b88cb 2023-07-10 thomas
7195 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo,
7196 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(commit));
7197 795b88cb 2023-07-10 thomas if (err)
7198 795b88cb 2023-07-10 thomas goto done;
7199 795b88cb 2023-07-10 thomas
7200 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7201 795b88cb 2023-07-10 thomas if (pid != NULL) {
7202 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&pcommit, repo, &pid->id);
7203 795b88cb 2023-07-10 thomas if (err)
7204 795b88cb 2023-07-10 thomas goto done;
7205 795b88cb 2023-07-10 thomas
7206 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&ptree, repo,
7207 795b88cb 2023-07-10 thomas got_object_commit_get_tree_id(pcommit));
7208 795b88cb 2023-07-10 thomas if (err)
7209 795b88cb 2023-07-10 thomas goto done;
7210 795b88cb 2023-07-10 thomas }
7211 795b88cb 2023-07-10 thomas
7212 795b88cb 2023-07-10 thomas err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
7213 795b88cb 2023-07-10 thomas got_diff_tree_collect_changed_paths, &paths, 0);
7214 795b88cb 2023-07-10 thomas if (err)
7215 795b88cb 2023-07-10 thomas goto done;
7216 795b88cb 2023-07-10 thomas
7217 795b88cb 2023-07-10 thomas err = got_worktree_status(worktree, &paths, repo, 0,
7218 795b88cb 2023-07-10 thomas worktree_has_commitable_path, wcpa, check_cancelled, NULL);
7219 795b88cb 2023-07-10 thomas if (err && err->code == GOT_ERR_FILE_MODIFIED) {
7220 795b88cb 2023-07-10 thomas /*
7221 795b88cb 2023-07-10 thomas * At least one changed path in the referenced commit is
7222 795b88cb 2023-07-10 thomas * modified in the work tree, that's all we need to know!
7223 795b88cb 2023-07-10 thomas */
7224 795b88cb 2023-07-10 thomas err = NULL;
7225 795b88cb 2023-07-10 thomas }
7226 795b88cb 2023-07-10 thomas
7227 795b88cb 2023-07-10 thomas done:
7228 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
7229 795b88cb 2023-07-10 thomas if (commit)
7230 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7231 795b88cb 2023-07-10 thomas if (pcommit)
7232 795b88cb 2023-07-10 thomas got_object_commit_close(pcommit);
7233 795b88cb 2023-07-10 thomas if (tree)
7234 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
7235 795b88cb 2023-07-10 thomas if (ptree)
7236 795b88cb 2023-07-10 thomas got_object_tree_close(ptree);
7237 795b88cb 2023-07-10 thomas return err;
7238 795b88cb 2023-07-10 thomas }
7239 795b88cb 2023-07-10 thomas
7240 795b88cb 2023-07-10 thomas /*
7241 795b88cb 2023-07-10 thomas * Remove any "logmsg" reference comprised entirely of paths that have
7242 795b88cb 2023-07-10 thomas * been reverted in this work tree. If any path in the logmsg ref changeset
7243 795b88cb 2023-07-10 thomas * remains in a changed state in the worktree, do not remove the reference.
7244 795b88cb 2023-07-10 thomas */
7245 795b88cb 2023-07-10 thomas static const struct got_error *
7246 795b88cb 2023-07-10 thomas rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
7247 795b88cb 2023-07-10 thomas {
7248 795b88cb 2023-07-10 thomas const struct got_error *err;
7249 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7250 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
7251 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
7252 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
7253 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
7254 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
7255 795b88cb 2023-07-10 thomas
7256 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7257 795b88cb 2023-07-10 thomas
7258 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
7259 795b88cb 2023-07-10 thomas if (err)
7260 795b88cb 2023-07-10 thomas goto done;
7261 795b88cb 2023-07-10 thomas
7262 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
7263 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
7264 795b88cb 2023-07-10 thomas if (err)
7265 795b88cb 2023-07-10 thomas goto done;
7266 795b88cb 2023-07-10 thomas
7267 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7268 795b88cb 2023-07-10 thomas const char *refname;
7269 795b88cb 2023-07-10 thomas int has_changes = 0;
7270 795b88cb 2023-07-10 thomas
7271 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7272 795b88cb 2023-07-10 thomas
7273 795b88cb 2023-07-10 thomas if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7274 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
7275 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7276 795b88cb 2023-07-10 thomas else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7277 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
7278 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7279 795b88cb 2023-07-10 thomas else
7280 795b88cb 2023-07-10 thomas continue;
7281 795b88cb 2023-07-10 thomas
7282 795b88cb 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7283 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7284 795b88cb 2023-07-10 thomas else
7285 795b88cb 2023-07-10 thomas continue;
7286 795b88cb 2023-07-10 thomas
7287 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&commit_id, NULL, refname,
7288 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
7289 795b88cb 2023-07-10 thomas if (err)
7290 795b88cb 2023-07-10 thomas goto done;
7291 795b88cb 2023-07-10 thomas
7292 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, commit_id);
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 wcpa.commit_paths = NULL;
7297 795b88cb 2023-07-10 thomas wcpa.has_changes = &has_changes;
7298 795b88cb 2023-07-10 thomas
7299 795b88cb 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, commit_id,
7300 795b88cb 2023-07-10 thomas worktree, repo);
7301 795b88cb 2023-07-10 thomas if (err)
7302 795b88cb 2023-07-10 thomas goto done;
7303 795b88cb 2023-07-10 thomas
7304 795b88cb 2023-07-10 thomas if (!has_changes) {
7305 795b88cb 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
7306 795b88cb 2023-07-10 thomas if (err)
7307 795b88cb 2023-07-10 thomas goto done;
7308 795b88cb 2023-07-10 thomas }
7309 795b88cb 2023-07-10 thomas
7310 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7311 795b88cb 2023-07-10 thomas commit = NULL;
7312 795b88cb 2023-07-10 thomas free(commit_id);
7313 795b88cb 2023-07-10 thomas commit_id = NULL;
7314 795b88cb 2023-07-10 thomas }
7315 795b88cb 2023-07-10 thomas
7316 795b88cb 2023-07-10 thomas done:
7317 795b88cb 2023-07-10 thomas free(uuidstr);
7318 795b88cb 2023-07-10 thomas free(commit_id);
7319 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
7320 795b88cb 2023-07-10 thomas if (commit)
7321 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7322 795b88cb 2023-07-10 thomas return err;
7323 795b88cb 2023-07-10 thomas }
7324 795b88cb 2023-07-10 thomas
7325 795b88cb 2023-07-10 thomas static const struct got_error *
7326 795b88cb 2023-07-10 thomas cmd_revert(int argc, char *argv[])
7327 795b88cb 2023-07-10 thomas {
7328 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
7329 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
7330 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
7331 795b88cb 2023-07-10 thomas char *cwd = NULL, *path = NULL;
7332 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7333 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7334 795b88cb 2023-07-10 thomas int ch, can_recurse = 0, pflag = 0;
7335 795b88cb 2023-07-10 thomas FILE *patch_script_file = NULL;
7336 795b88cb 2023-07-10 thomas const char *patch_script_path = NULL;
7337 795b88cb 2023-07-10 thomas struct choose_patch_arg cpa;
7338 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
7339 795b88cb 2023-07-10 thomas
7340 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7341 795b88cb 2023-07-10 thomas
7342 795b88cb 2023-07-10 thomas #ifndef PROFILE
7343 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7344 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
7345 795b88cb 2023-07-10 thomas err(1, "pledge");
7346 795b88cb 2023-07-10 thomas #endif
7347 795b88cb 2023-07-10 thomas
7348 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "F:pR")) != -1) {
7349 795b88cb 2023-07-10 thomas switch (ch) {
7350 795b88cb 2023-07-10 thomas case 'F':
7351 795b88cb 2023-07-10 thomas patch_script_path = optarg;
7352 795b88cb 2023-07-10 thomas break;
7353 795b88cb 2023-07-10 thomas case 'p':
7354 795b88cb 2023-07-10 thomas pflag = 1;
7355 795b88cb 2023-07-10 thomas break;
7356 795b88cb 2023-07-10 thomas case 'R':
7357 795b88cb 2023-07-10 thomas can_recurse = 1;
7358 795b88cb 2023-07-10 thomas break;
7359 795b88cb 2023-07-10 thomas default:
7360 795b88cb 2023-07-10 thomas usage_revert();
7361 795b88cb 2023-07-10 thomas /* NOTREACHED */
7362 795b88cb 2023-07-10 thomas }
7363 795b88cb 2023-07-10 thomas }
7364 795b88cb 2023-07-10 thomas
7365 795b88cb 2023-07-10 thomas argc -= optind;
7366 795b88cb 2023-07-10 thomas argv += optind;
7367 795b88cb 2023-07-10 thomas
7368 795b88cb 2023-07-10 thomas if (argc < 1)
7369 795b88cb 2023-07-10 thomas usage_revert();
7370 795b88cb 2023-07-10 thomas if (patch_script_path && !pflag)
7371 795b88cb 2023-07-10 thomas errx(1, "-F option can only be used together with -p option");
7372 795b88cb 2023-07-10 thomas
7373 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
7374 795b88cb 2023-07-10 thomas if (cwd == NULL) {
7375 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
7376 795b88cb 2023-07-10 thomas goto done;
7377 795b88cb 2023-07-10 thomas }
7378 795b88cb 2023-07-10 thomas
7379 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7380 795b88cb 2023-07-10 thomas if (error != NULL)
7381 795b88cb 2023-07-10 thomas goto done;
7382 795b88cb 2023-07-10 thomas
7383 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
7384 795b88cb 2023-07-10 thomas if (error) {
7385 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7386 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "revert", cwd);
7387 795b88cb 2023-07-10 thomas goto done;
7388 795b88cb 2023-07-10 thomas }
7389 795b88cb 2023-07-10 thomas
7390 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7391 795b88cb 2023-07-10 thomas NULL, pack_fds);
7392 795b88cb 2023-07-10 thomas if (error != NULL)
7393 795b88cb 2023-07-10 thomas goto done;
7394 795b88cb 2023-07-10 thomas
7395 795b88cb 2023-07-10 thomas if (patch_script_path) {
7396 795b88cb 2023-07-10 thomas patch_script_file = fopen(patch_script_path, "re");
7397 795b88cb 2023-07-10 thomas if (patch_script_file == NULL) {
7398 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fopen",
7399 795b88cb 2023-07-10 thomas patch_script_path);
7400 795b88cb 2023-07-10 thomas goto done;
7401 795b88cb 2023-07-10 thomas }
7402 795b88cb 2023-07-10 thomas }
7403 795b88cb 2023-07-10 thomas
7404 795b88cb 2023-07-10 thomas /*
7405 795b88cb 2023-07-10 thomas * XXX "c" perm needed on repo dir to delete merge references.
7406 795b88cb 2023-07-10 thomas */
7407 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
7408 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
7409 795b88cb 2023-07-10 thomas if (error)
7410 795b88cb 2023-07-10 thomas goto done;
7411 795b88cb 2023-07-10 thomas
7412 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, 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 if (!can_recurse) {
7417 795b88cb 2023-07-10 thomas char *ondisk_path;
7418 795b88cb 2023-07-10 thomas struct stat sb;
7419 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
7420 795b88cb 2023-07-10 thomas if (asprintf(&ondisk_path, "%s/%s",
7421 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree),
7422 795b88cb 2023-07-10 thomas pe->path) == -1) {
7423 795b88cb 2023-07-10 thomas error = got_error_from_errno("asprintf");
7424 795b88cb 2023-07-10 thomas goto done;
7425 795b88cb 2023-07-10 thomas }
7426 795b88cb 2023-07-10 thomas if (lstat(ondisk_path, &sb) == -1) {
7427 795b88cb 2023-07-10 thomas if (errno == ENOENT) {
7428 795b88cb 2023-07-10 thomas free(ondisk_path);
7429 795b88cb 2023-07-10 thomas continue;
7430 795b88cb 2023-07-10 thomas }
7431 795b88cb 2023-07-10 thomas error = got_error_from_errno2("lstat",
7432 795b88cb 2023-07-10 thomas ondisk_path);
7433 795b88cb 2023-07-10 thomas free(ondisk_path);
7434 795b88cb 2023-07-10 thomas goto done;
7435 795b88cb 2023-07-10 thomas }
7436 795b88cb 2023-07-10 thomas free(ondisk_path);
7437 795b88cb 2023-07-10 thomas if (S_ISDIR(sb.st_mode)) {
7438 795b88cb 2023-07-10 thomas error = got_error_msg(GOT_ERR_BAD_PATH,
7439 795b88cb 2023-07-10 thomas "reverting directories requires -R option");
7440 795b88cb 2023-07-10 thomas goto done;
7441 795b88cb 2023-07-10 thomas }
7442 795b88cb 2023-07-10 thomas }
7443 795b88cb 2023-07-10 thomas }
7444 795b88cb 2023-07-10 thomas
7445 795b88cb 2023-07-10 thomas cpa.patch_script_file = patch_script_file;
7446 795b88cb 2023-07-10 thomas cpa.action = "revert";
7447 795b88cb 2023-07-10 thomas error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7448 795b88cb 2023-07-10 thomas pflag ? choose_patch : NULL, &cpa, repo);
7449 795b88cb 2023-07-10 thomas
7450 795b88cb 2023-07-10 thomas error = rm_logmsg_ref(worktree, repo);
7451 795b88cb 2023-07-10 thomas done:
7452 795b88cb 2023-07-10 thomas if (patch_script_file && fclose(patch_script_file) == EOF &&
7453 795b88cb 2023-07-10 thomas error == NULL)
7454 795b88cb 2023-07-10 thomas error = got_error_from_errno2("fclose", patch_script_path);
7455 795b88cb 2023-07-10 thomas if (repo) {
7456 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
7457 795b88cb 2023-07-10 thomas if (error == NULL)
7458 795b88cb 2023-07-10 thomas error = close_err;
7459 795b88cb 2023-07-10 thomas }
7460 795b88cb 2023-07-10 thomas if (worktree)
7461 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
7462 795b88cb 2023-07-10 thomas if (pack_fds) {
7463 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
7464 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
7465 795b88cb 2023-07-10 thomas if (error == NULL)
7466 795b88cb 2023-07-10 thomas error = pack_err;
7467 795b88cb 2023-07-10 thomas }
7468 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7469 795b88cb 2023-07-10 thomas free(path);
7470 795b88cb 2023-07-10 thomas free(cwd);
7471 795b88cb 2023-07-10 thomas return error;
7472 795b88cb 2023-07-10 thomas }
7473 795b88cb 2023-07-10 thomas
7474 795b88cb 2023-07-10 thomas __dead static void
7475 795b88cb 2023-07-10 thomas usage_commit(void)
7476 795b88cb 2023-07-10 thomas {
7477 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
7478 795b88cb 2023-07-10 thomas "[-m message] [path ...]\n", getprogname());
7479 795b88cb 2023-07-10 thomas exit(1);
7480 795b88cb 2023-07-10 thomas }
7481 795b88cb 2023-07-10 thomas
7482 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg {
7483 795b88cb 2023-07-10 thomas const char *cmdline_log;
7484 795b88cb 2023-07-10 thomas const char *prepared_log;
7485 795b88cb 2023-07-10 thomas const char *merged_log;
7486 795b88cb 2023-07-10 thomas int non_interactive;
7487 795b88cb 2023-07-10 thomas const char *editor;
7488 795b88cb 2023-07-10 thomas const char *worktree_path;
7489 795b88cb 2023-07-10 thomas const char *repo_path;
7490 795b88cb 2023-07-10 thomas const char *dial_proto;
7491 795b88cb 2023-07-10 thomas char *logmsg_path;
7492 795b88cb 2023-07-10 thomas };
7493 795b88cb 2023-07-10 thomas
7494 795b88cb 2023-07-10 thomas static const struct got_error *
7495 795b88cb 2023-07-10 thomas read_prepared_logmsg(char **logmsg, const char *path)
7496 795b88cb 2023-07-10 thomas {
7497 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7498 795b88cb 2023-07-10 thomas FILE *f = NULL;
7499 795b88cb 2023-07-10 thomas struct stat sb;
7500 795b88cb 2023-07-10 thomas size_t r;
7501 795b88cb 2023-07-10 thomas
7502 795b88cb 2023-07-10 thomas *logmsg = NULL;
7503 795b88cb 2023-07-10 thomas memset(&sb, 0, sizeof(sb));
7504 795b88cb 2023-07-10 thomas
7505 795b88cb 2023-07-10 thomas f = fopen(path, "re");
7506 795b88cb 2023-07-10 thomas if (f == NULL)
7507 795b88cb 2023-07-10 thomas return got_error_from_errno2("fopen", path);
7508 795b88cb 2023-07-10 thomas
7509 795b88cb 2023-07-10 thomas if (fstat(fileno(f), &sb) == -1) {
7510 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fstat", path);
7511 795b88cb 2023-07-10 thomas goto done;
7512 795b88cb 2023-07-10 thomas }
7513 795b88cb 2023-07-10 thomas if (sb.st_size == 0) {
7514 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7515 795b88cb 2023-07-10 thomas goto done;
7516 795b88cb 2023-07-10 thomas }
7517 795b88cb 2023-07-10 thomas
7518 795b88cb 2023-07-10 thomas *logmsg = malloc(sb.st_size + 1);
7519 795b88cb 2023-07-10 thomas if (*logmsg == NULL) {
7520 795b88cb 2023-07-10 thomas err = got_error_from_errno("malloc");
7521 795b88cb 2023-07-10 thomas goto done;
7522 795b88cb 2023-07-10 thomas }
7523 795b88cb 2023-07-10 thomas
7524 795b88cb 2023-07-10 thomas r = fread(*logmsg, 1, sb.st_size, f);
7525 795b88cb 2023-07-10 thomas if (r != sb.st_size) {
7526 795b88cb 2023-07-10 thomas if (ferror(f))
7527 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fread", path);
7528 795b88cb 2023-07-10 thomas else
7529 795b88cb 2023-07-10 thomas err = got_error(GOT_ERR_IO);
7530 795b88cb 2023-07-10 thomas goto done;
7531 795b88cb 2023-07-10 thomas }
7532 795b88cb 2023-07-10 thomas (*logmsg)[sb.st_size] = '\0';
7533 795b88cb 2023-07-10 thomas done:
7534 795b88cb 2023-07-10 thomas if (fclose(f) == EOF && err == NULL)
7535 795b88cb 2023-07-10 thomas err = got_error_from_errno2("fclose", path);
7536 795b88cb 2023-07-10 thomas if (err) {
7537 795b88cb 2023-07-10 thomas free(*logmsg);
7538 795b88cb 2023-07-10 thomas *logmsg = NULL;
7539 795b88cb 2023-07-10 thomas }
7540 795b88cb 2023-07-10 thomas return err;
7541 795b88cb 2023-07-10 thomas }
7542 795b88cb 2023-07-10 thomas
7543 795b88cb 2023-07-10 thomas static const struct got_error *
7544 795b88cb 2023-07-10 thomas collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
7545 795b88cb 2023-07-10 thomas const char *diff_path, char **logmsg, void *arg)
7546 795b88cb 2023-07-10 thomas {
7547 795b88cb 2023-07-10 thomas char *initial_content = NULL;
7548 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
7549 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7550 795b88cb 2023-07-10 thomas char *template = NULL;
7551 795b88cb 2023-07-10 thomas char *prepared_msg = NULL, *merged_msg = NULL;
7552 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg *a = arg;
7553 795b88cb 2023-07-10 thomas int initial_content_len;
7554 795b88cb 2023-07-10 thomas int fd = -1;
7555 795b88cb 2023-07-10 thomas size_t len;
7556 795b88cb 2023-07-10 thomas
7557 795b88cb 2023-07-10 thomas /* if a message was specified on the command line, just use it */
7558 795b88cb 2023-07-10 thomas if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
7559 795b88cb 2023-07-10 thomas len = strlen(a->cmdline_log) + 1;
7560 795b88cb 2023-07-10 thomas *logmsg = malloc(len + 1);
7561 795b88cb 2023-07-10 thomas if (*logmsg == NULL)
7562 795b88cb 2023-07-10 thomas return got_error_from_errno("malloc");
7563 795b88cb 2023-07-10 thomas strlcpy(*logmsg, a->cmdline_log, len);
7564 795b88cb 2023-07-10 thomas return NULL;
7565 795b88cb 2023-07-10 thomas } else if (a->prepared_log != NULL && a->non_interactive)
7566 795b88cb 2023-07-10 thomas return read_prepared_logmsg(logmsg, a->prepared_log);
7567 795b88cb 2023-07-10 thomas
7568 795b88cb 2023-07-10 thomas if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7569 795b88cb 2023-07-10 thomas return got_error_from_errno("asprintf");
7570 795b88cb 2023-07-10 thomas
7571 795b88cb 2023-07-10 thomas err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
7572 795b88cb 2023-07-10 thomas if (err)
7573 795b88cb 2023-07-10 thomas goto done;
7574 795b88cb 2023-07-10 thomas
7575 795b88cb 2023-07-10 thomas if (a->prepared_log) {
7576 795b88cb 2023-07-10 thomas err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
7577 795b88cb 2023-07-10 thomas if (err)
7578 795b88cb 2023-07-10 thomas goto done;
7579 795b88cb 2023-07-10 thomas } else if (a->merged_log) {
7580 795b88cb 2023-07-10 thomas err = read_prepared_logmsg(&merged_msg, a->merged_log);
7581 795b88cb 2023-07-10 thomas if (err)
7582 795b88cb 2023-07-10 thomas goto done;
7583 795b88cb 2023-07-10 thomas }
7584 795b88cb 2023-07-10 thomas
7585 795b88cb 2023-07-10 thomas initial_content_len = asprintf(&initial_content,
7586 795b88cb 2023-07-10 thomas "%s%s\n# changes to be committed:\n",
7587 795b88cb 2023-07-10 thomas prepared_msg ? prepared_msg : "",
7588 795b88cb 2023-07-10 thomas merged_msg ? merged_msg : "");
7589 795b88cb 2023-07-10 thomas if (initial_content_len == -1) {
7590 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
7591 795b88cb 2023-07-10 thomas goto done;
7592 795b88cb 2023-07-10 thomas }
7593 795b88cb 2023-07-10 thomas
7594 795b88cb 2023-07-10 thomas if (write(fd, initial_content, initial_content_len) == -1) {
7595 795b88cb 2023-07-10 thomas err = got_error_from_errno2("write", a->logmsg_path);
7596 795b88cb 2023-07-10 thomas goto done;
7597 795b88cb 2023-07-10 thomas }
7598 795b88cb 2023-07-10 thomas
7599 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, commitable_paths, entry) {
7600 795b88cb 2023-07-10 thomas struct got_commitable *ct = pe->data;
7601 795b88cb 2023-07-10 thomas dprintf(fd, "# %c %s\n",
7602 795b88cb 2023-07-10 thomas got_commitable_get_status(ct),
7603 795b88cb 2023-07-10 thomas got_commitable_get_path(ct));
7604 795b88cb 2023-07-10 thomas }
7605 795b88cb 2023-07-10 thomas
7606 795b88cb 2023-07-10 thomas if (diff_path) {
7607 795b88cb 2023-07-10 thomas dprintf(fd, "# detailed changes can be viewed in %s\n",
7608 795b88cb 2023-07-10 thomas diff_path);
7609 795b88cb 2023-07-10 thomas }
7610 795b88cb 2023-07-10 thomas
7611 795b88cb 2023-07-10 thomas if (close(fd) == -1) {
7612 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
7613 795b88cb 2023-07-10 thomas goto done;
7614 795b88cb 2023-07-10 thomas }
7615 795b88cb 2023-07-10 thomas fd = -1;
7616 795b88cb 2023-07-10 thomas
7617 795b88cb 2023-07-10 thomas err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7618 795b88cb 2023-07-10 thomas initial_content_len, a->prepared_log ? 0 : 1);
7619 795b88cb 2023-07-10 thomas done:
7620 795b88cb 2023-07-10 thomas free(initial_content);
7621 795b88cb 2023-07-10 thomas free(template);
7622 795b88cb 2023-07-10 thomas free(prepared_msg);
7623 795b88cb 2023-07-10 thomas free(merged_msg);
7624 795b88cb 2023-07-10 thomas
7625 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
7626 795b88cb 2023-07-10 thomas err = got_error_from_errno2("close", a->logmsg_path);
7627 795b88cb 2023-07-10 thomas
7628 795b88cb 2023-07-10 thomas /* Editor is done; we can now apply unveil(2) */
7629 795b88cb 2023-07-10 thomas if (err == NULL)
7630 795b88cb 2023-07-10 thomas err = got_dial_apply_unveil(a->dial_proto);
7631 795b88cb 2023-07-10 thomas if (err == NULL)
7632 795b88cb 2023-07-10 thomas err = apply_unveil(a->repo_path, 0, a->worktree_path);
7633 795b88cb 2023-07-10 thomas if (err) {
7634 795b88cb 2023-07-10 thomas free(*logmsg);
7635 795b88cb 2023-07-10 thomas *logmsg = NULL;
7636 795b88cb 2023-07-10 thomas }
7637 795b88cb 2023-07-10 thomas return err;
7638 795b88cb 2023-07-10 thomas }
7639 795b88cb 2023-07-10 thomas
7640 795b88cb 2023-07-10 thomas static const struct got_error *
7641 795b88cb 2023-07-10 thomas cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
7642 795b88cb 2023-07-10 thomas const char *type, int has_content)
7643 795b88cb 2023-07-10 thomas {
7644 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
7645 795b88cb 2023-07-10 thomas char *logmsg = NULL;
7646 795b88cb 2023-07-10 thomas
7647 795b88cb 2023-07-10 thomas err = got_object_commit_get_logmsg(&logmsg, commit);
7648 795b88cb 2023-07-10 thomas if (err)
7649 795b88cb 2023-07-10 thomas return err;
7650 795b88cb 2023-07-10 thomas
7651 795b88cb 2023-07-10 thomas if (fprintf(f, "%s# log message of %s commit %s:%s",
7652 795b88cb 2023-07-10 thomas has_content ? "\n" : "", type, idstr, logmsg) < 0)
7653 795b88cb 2023-07-10 thomas err = got_ferror(f, GOT_ERR_IO);
7654 795b88cb 2023-07-10 thomas
7655 795b88cb 2023-07-10 thomas free(logmsg);
7656 795b88cb 2023-07-10 thomas return err;
7657 795b88cb 2023-07-10 thomas }
7658 795b88cb 2023-07-10 thomas
7659 795b88cb 2023-07-10 thomas /*
7660 795b88cb 2023-07-10 thomas * Lookup "logmsg" references of backed-out and cherrypicked commits
7661 795b88cb 2023-07-10 thomas * belonging to the current work tree. If found, and the worktree has
7662 795b88cb 2023-07-10 thomas * at least one modified file that was changed in the referenced commit,
7663 795b88cb 2023-07-10 thomas * add its log message to a new temporary file at *logmsg_path.
7664 795b88cb 2023-07-10 thomas * Add all refs found to matched_refs to be scheduled for removal on
7665 795b88cb 2023-07-10 thomas * successful commit.
7666 795b88cb 2023-07-10 thomas */
7667 795b88cb 2023-07-10 thomas static const struct got_error *
7668 795b88cb 2023-07-10 thomas lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
7669 795b88cb 2023-07-10 thomas struct got_reflist_head *matched_refs, struct got_worktree *worktree,
7670 795b88cb 2023-07-10 thomas struct got_repository *repo)
7671 795b88cb 2023-07-10 thomas {
7672 795b88cb 2023-07-10 thomas const struct got_error *err;
7673 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
7674 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
7675 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7676 795b88cb 2023-07-10 thomas struct got_reflist_entry *re, *re_match;
7677 795b88cb 2023-07-10 thomas FILE *f = NULL;
7678 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
7679 795b88cb 2023-07-10 thomas int added_logmsg = 0;
7680 795b88cb 2023-07-10 thomas
7681 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7682 795b88cb 2023-07-10 thomas
7683 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
7684 795b88cb 2023-07-10 thomas
7685 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
7686 795b88cb 2023-07-10 thomas if (err)
7687 795b88cb 2023-07-10 thomas goto done;
7688 795b88cb 2023-07-10 thomas
7689 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, "refs/got/worktree",
7690 795b88cb 2023-07-10 thomas got_ref_cmp_by_name, repo);
7691 795b88cb 2023-07-10 thomas if (err)
7692 795b88cb 2023-07-10 thomas goto done;
7693 795b88cb 2023-07-10 thomas
7694 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
7695 795b88cb 2023-07-10 thomas const char *refname, *type;
7696 795b88cb 2023-07-10 thomas struct wt_commitable_path_arg wcpa;
7697 795b88cb 2023-07-10 thomas int add_logmsg = 0;
7698 795b88cb 2023-07-10 thomas
7699 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
7700 795b88cb 2023-07-10 thomas
7701 795b88cb 2023-07-10 thomas if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
7702 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
7703 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
7704 795b88cb 2023-07-10 thomas type = "cherrypicked";
7705 795b88cb 2023-07-10 thomas } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
7706 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
7707 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
7708 795b88cb 2023-07-10 thomas type = "backed-out";
7709 795b88cb 2023-07-10 thomas } else
7710 795b88cb 2023-07-10 thomas continue;
7711 795b88cb 2023-07-10 thomas
7712 795b88cb 2023-07-10 thomas if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
7713 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
7714 795b88cb 2023-07-10 thomas else
7715 795b88cb 2023-07-10 thomas continue;
7716 795b88cb 2023-07-10 thomas
7717 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
7718 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
7719 795b88cb 2023-07-10 thomas if (err)
7720 795b88cb 2023-07-10 thomas goto done;
7721 795b88cb 2023-07-10 thomas
7722 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
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 wcpa.commit_paths = paths;
7727 795b88cb 2023-07-10 thomas wcpa.has_changes = &add_logmsg;
7728 795b88cb 2023-07-10 thomas
7729 795b88cb 2023-07-10 thomas err = commit_path_changed_in_worktree(&wcpa, id,
7730 795b88cb 2023-07-10 thomas worktree, repo);
7731 795b88cb 2023-07-10 thomas if (err)
7732 795b88cb 2023-07-10 thomas goto done;
7733 795b88cb 2023-07-10 thomas
7734 795b88cb 2023-07-10 thomas if (add_logmsg) {
7735 795b88cb 2023-07-10 thomas if (f == NULL) {
7736 795b88cb 2023-07-10 thomas err = got_opentemp_named(logmsg_path, &f,
7737 795b88cb 2023-07-10 thomas "got-commit-logmsg", "");
7738 795b88cb 2023-07-10 thomas if (err)
7739 795b88cb 2023-07-10 thomas goto done;
7740 795b88cb 2023-07-10 thomas }
7741 795b88cb 2023-07-10 thomas err = cat_logmsg(f, commit, refname, type,
7742 795b88cb 2023-07-10 thomas added_logmsg);
7743 795b88cb 2023-07-10 thomas if (err)
7744 795b88cb 2023-07-10 thomas goto done;
7745 795b88cb 2023-07-10 thomas if (!added_logmsg)
7746 795b88cb 2023-07-10 thomas ++added_logmsg;
7747 795b88cb 2023-07-10 thomas
7748 795b88cb 2023-07-10 thomas err = got_reflist_entry_dup(&re_match, re);
7749 795b88cb 2023-07-10 thomas if (err)
7750 795b88cb 2023-07-10 thomas goto done;
7751 795b88cb 2023-07-10 thomas TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
7752 795b88cb 2023-07-10 thomas }
7753 795b88cb 2023-07-10 thomas
7754 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7755 795b88cb 2023-07-10 thomas commit = NULL;
7756 795b88cb 2023-07-10 thomas free(id);
7757 795b88cb 2023-07-10 thomas id = NULL;
7758 795b88cb 2023-07-10 thomas }
7759 795b88cb 2023-07-10 thomas
7760 795b88cb 2023-07-10 thomas done:
7761 795b88cb 2023-07-10 thomas free(id);
7762 795b88cb 2023-07-10 thomas free(uuidstr);
7763 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
7764 795b88cb 2023-07-10 thomas if (commit)
7765 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
7766 795b88cb 2023-07-10 thomas if (f && fclose(f) == EOF && err == NULL)
7767 795b88cb 2023-07-10 thomas err = got_error_from_errno("fclose");
7768 795b88cb 2023-07-10 thomas if (!added_logmsg) {
7769 795b88cb 2023-07-10 thomas if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
7770 795b88cb 2023-07-10 thomas err = got_error_from_errno2("unlink", *logmsg_path);
7771 795b88cb 2023-07-10 thomas *logmsg_path = NULL;
7772 795b88cb 2023-07-10 thomas }
7773 795b88cb 2023-07-10 thomas return err;
7774 795b88cb 2023-07-10 thomas }
7775 795b88cb 2023-07-10 thomas
7776 795b88cb 2023-07-10 thomas static const struct got_error *
7777 795b88cb 2023-07-10 thomas cmd_commit(int argc, char *argv[])
7778 795b88cb 2023-07-10 thomas {
7779 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
7780 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
7781 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
7782 795b88cb 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
7783 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
7784 795b88cb 2023-07-10 thomas const char *logmsg = NULL;
7785 795b88cb 2023-07-10 thomas char *prepared_logmsg = NULL, *merged_logmsg = NULL;
7786 795b88cb 2023-07-10 thomas struct collect_commit_logmsg_arg cl_arg;
7787 795b88cb 2023-07-10 thomas const char *author = NULL;
7788 795b88cb 2023-07-10 thomas char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
7789 795b88cb 2023-07-10 thomas int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7790 795b88cb 2023-07-10 thomas int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7791 795b88cb 2023-07-10 thomas int show_diff = 1, commit_conflicts = 0;
7792 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
7793 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
7794 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
7795 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
7796 795b88cb 2023-07-10 thomas const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7797 795b88cb 2023-07-10 thomas const struct got_remote_repo *remotes, *remote = NULL;
7798 795b88cb 2023-07-10 thomas int nremotes;
7799 795b88cb 2023-07-10 thomas char *proto = NULL, *host = NULL, *port = NULL;
7800 795b88cb 2023-07-10 thomas char *repo_name = NULL, *server_path = NULL;
7801 795b88cb 2023-07-10 thomas const char *remote_name;
7802 795b88cb 2023-07-10 thomas int verbosity = 0;
7803 795b88cb 2023-07-10 thomas int i;
7804 795b88cb 2023-07-10 thomas
7805 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
7806 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
7807 795b88cb 2023-07-10 thomas cl_arg.logmsg_path = NULL;
7808 795b88cb 2023-07-10 thomas
7809 795b88cb 2023-07-10 thomas #ifndef PROFILE
7810 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7811 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
7812 795b88cb 2023-07-10 thomas err(1, "pledge");
7813 795b88cb 2023-07-10 thomas #endif
7814 795b88cb 2023-07-10 thomas
7815 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
7816 795b88cb 2023-07-10 thomas switch (ch) {
7817 795b88cb 2023-07-10 thomas case 'A':
7818 795b88cb 2023-07-10 thomas author = optarg;
7819 795b88cb 2023-07-10 thomas error = valid_author(author);
7820 795b88cb 2023-07-10 thomas if (error)
7821 795b88cb 2023-07-10 thomas return error;
7822 795b88cb 2023-07-10 thomas break;
7823 795b88cb 2023-07-10 thomas case 'C':
7824 795b88cb 2023-07-10 thomas commit_conflicts = 1;
7825 795b88cb 2023-07-10 thomas break;
7826 795b88cb 2023-07-10 thomas case 'F':
7827 795b88cb 2023-07-10 thomas if (logmsg != NULL)
7828 795b88cb 2023-07-10 thomas option_conflict('F', 'm');
7829 795b88cb 2023-07-10 thomas prepared_logmsg = realpath(optarg, NULL);
7830 795b88cb 2023-07-10 thomas if (prepared_logmsg == NULL)
7831 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
7832 795b88cb 2023-07-10 thomas optarg);
7833 795b88cb 2023-07-10 thomas break;
7834 795b88cb 2023-07-10 thomas case 'm':
7835 795b88cb 2023-07-10 thomas if (prepared_logmsg)
7836 795b88cb 2023-07-10 thomas option_conflict('m', 'F');
7837 795b88cb 2023-07-10 thomas logmsg = optarg;
7838 795b88cb 2023-07-10 thomas break;
7839 795b88cb 2023-07-10 thomas case 'N':
7840 795b88cb 2023-07-10 thomas non_interactive = 1;
7841 795b88cb 2023-07-10 thomas break;
7842 795b88cb 2023-07-10 thomas case 'n':
7843 795b88cb 2023-07-10 thomas show_diff = 0;
7844 795b88cb 2023-07-10 thomas break;
7845 795b88cb 2023-07-10 thomas case 'S':
7846 795b88cb 2023-07-10 thomas allow_bad_symlinks = 1;
7847 795b88cb 2023-07-10 thomas break;
7848 795b88cb 2023-07-10 thomas default:
7849 795b88cb 2023-07-10 thomas usage_commit();
7850 795b88cb 2023-07-10 thomas /* NOTREACHED */
7851 795b88cb 2023-07-10 thomas }
7852 795b88cb 2023-07-10 thomas }
7853 795b88cb 2023-07-10 thomas
7854 795b88cb 2023-07-10 thomas argc -= optind;
7855 795b88cb 2023-07-10 thomas argv += optind;
7856 795b88cb 2023-07-10 thomas
7857 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
7858 795b88cb 2023-07-10 thomas if (cwd == NULL) {
7859 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
7860 795b88cb 2023-07-10 thomas goto done;
7861 795b88cb 2023-07-10 thomas }
7862 795b88cb 2023-07-10 thomas
7863 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
7864 795b88cb 2023-07-10 thomas if (error != NULL)
7865 795b88cb 2023-07-10 thomas goto done;
7866 795b88cb 2023-07-10 thomas
7867 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
7868 795b88cb 2023-07-10 thomas if (error) {
7869 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
7870 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "commit", cwd);
7871 795b88cb 2023-07-10 thomas goto done;
7872 795b88cb 2023-07-10 thomas }
7873 795b88cb 2023-07-10 thomas
7874 795b88cb 2023-07-10 thomas error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7875 795b88cb 2023-07-10 thomas if (error)
7876 795b88cb 2023-07-10 thomas goto done;
7877 795b88cb 2023-07-10 thomas if (rebase_in_progress) {
7878 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_REBASING);
7879 795b88cb 2023-07-10 thomas goto done;
7880 795b88cb 2023-07-10 thomas }
7881 795b88cb 2023-07-10 thomas
7882 795b88cb 2023-07-10 thomas error = got_worktree_histedit_in_progress(&histedit_in_progress,
7883 795b88cb 2023-07-10 thomas worktree);
7884 795b88cb 2023-07-10 thomas if (error)
7885 795b88cb 2023-07-10 thomas goto done;
7886 795b88cb 2023-07-10 thomas
7887 795b88cb 2023-07-10 thomas error = get_gitconfig_path(&gitconfig_path);
7888 795b88cb 2023-07-10 thomas if (error)
7889 795b88cb 2023-07-10 thomas goto done;
7890 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7891 795b88cb 2023-07-10 thomas gitconfig_path, pack_fds);
7892 795b88cb 2023-07-10 thomas if (error != NULL)
7893 795b88cb 2023-07-10 thomas goto done;
7894 795b88cb 2023-07-10 thomas
7895 795b88cb 2023-07-10 thomas error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7896 795b88cb 2023-07-10 thomas if (error)
7897 795b88cb 2023-07-10 thomas goto done;
7898 795b88cb 2023-07-10 thomas if (merge_in_progress) {
7899 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_MERGE_BUSY);
7900 795b88cb 2023-07-10 thomas goto done;
7901 795b88cb 2023-07-10 thomas }
7902 795b88cb 2023-07-10 thomas
7903 795b88cb 2023-07-10 thomas error = get_author(&committer, repo, worktree);
7904 795b88cb 2023-07-10 thomas if (error)
7905 795b88cb 2023-07-10 thomas goto done;
7906 795b88cb 2023-07-10 thomas
7907 795b88cb 2023-07-10 thomas if (author == NULL)
7908 795b88cb 2023-07-10 thomas author = committer;
7909 795b88cb 2023-07-10 thomas
7910 795b88cb 2023-07-10 thomas remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7911 795b88cb 2023-07-10 thomas worktree_conf = got_worktree_get_gotconfig(worktree);
7912 795b88cb 2023-07-10 thomas if (worktree_conf) {
7913 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
7914 795b88cb 2023-07-10 thomas worktree_conf);
7915 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7916 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7917 795b88cb 2023-07-10 thomas remote = &remotes[i];
7918 795b88cb 2023-07-10 thomas break;
7919 795b88cb 2023-07-10 thomas }
7920 795b88cb 2023-07-10 thomas }
7921 795b88cb 2023-07-10 thomas }
7922 795b88cb 2023-07-10 thomas if (remote == NULL) {
7923 795b88cb 2023-07-10 thomas repo_conf = got_repo_get_gotconfig(repo);
7924 795b88cb 2023-07-10 thomas if (repo_conf) {
7925 795b88cb 2023-07-10 thomas got_gotconfig_get_remotes(&nremotes, &remotes,
7926 795b88cb 2023-07-10 thomas repo_conf);
7927 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7928 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7929 795b88cb 2023-07-10 thomas remote = &remotes[i];
7930 795b88cb 2023-07-10 thomas break;
7931 795b88cb 2023-07-10 thomas }
7932 795b88cb 2023-07-10 thomas }
7933 795b88cb 2023-07-10 thomas }
7934 795b88cb 2023-07-10 thomas }
7935 795b88cb 2023-07-10 thomas if (remote == NULL) {
7936 795b88cb 2023-07-10 thomas got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7937 795b88cb 2023-07-10 thomas for (i = 0; i < nremotes; i++) {
7938 795b88cb 2023-07-10 thomas if (strcmp(remotes[i].name, remote_name) == 0) {
7939 795b88cb 2023-07-10 thomas remote = &remotes[i];
7940 795b88cb 2023-07-10 thomas break;
7941 795b88cb 2023-07-10 thomas }
7942 795b88cb 2023-07-10 thomas }
7943 795b88cb 2023-07-10 thomas }
7944 795b88cb 2023-07-10 thomas if (remote == NULL) {
7945 795b88cb 2023-07-10 thomas error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7946 795b88cb 2023-07-10 thomas goto done;
7947 795b88cb 2023-07-10 thomas }
7948 795b88cb 2023-07-10 thomas
7949 795b88cb 2023-07-10 thomas error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7950 795b88cb 2023-07-10 thomas &repo_name, remote->fetch_url);
7951 795b88cb 2023-07-10 thomas if (error)
7952 795b88cb 2023-07-10 thomas goto done;
7953 795b88cb 2023-07-10 thomas
7954 795b88cb 2023-07-10 thomas if (strcmp(proto, "git") == 0) {
7955 795b88cb 2023-07-10 thomas #ifndef PROFILE
7956 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7957 795b88cb 2023-07-10 thomas "sendfd dns inet unveil", NULL) == -1)
7958 795b88cb 2023-07-10 thomas err(1, "pledge");
7959 795b88cb 2023-07-10 thomas #endif
7960 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "git+ssh") == 0 ||
7961 795b88cb 2023-07-10 thomas strcmp(proto, "ssh") == 0) {
7962 795b88cb 2023-07-10 thomas #ifndef PROFILE
7963 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7964 795b88cb 2023-07-10 thomas "sendfd unveil", NULL) == -1)
7965 795b88cb 2023-07-10 thomas err(1, "pledge");
7966 795b88cb 2023-07-10 thomas #endif
7967 795b88cb 2023-07-10 thomas } else if (strcmp(proto, "http") == 0 ||
7968 795b88cb 2023-07-10 thomas strcmp(proto, "git+http") == 0) {
7969 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7970 795b88cb 2023-07-10 thomas goto done;
7971 795b88cb 2023-07-10 thomas } else {
7972 795b88cb 2023-07-10 thomas error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7973 795b88cb 2023-07-10 thomas goto done;
7974 795b88cb 2023-07-10 thomas }
7975 795b88cb 2023-07-10 thomas
7976 795b88cb 2023-07-10 thomas
7977 795b88cb 2023-07-10 thomas /*if (verbosity >= 0) {
7978 795b88cb 2023-07-10 thomas printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
7979 795b88cb 2023-07-10 thomas remote->name, proto, host,
7980 795b88cb 2023-07-10 thomas port ? ":" : "", port ? port : "",
7981 795b88cb 2023-07-10 thomas *server_path == '/' ? "" : "/", server_path);
7982 795b88cb 2023-07-10 thomas }*/
7983 795b88cb 2023-07-10 thomas
7984 795b88cb 2023-07-10 thomas /*
7985 795b88cb 2023-07-10 thomas * unveil(2) traverses exec(2); if an editor is used we have
7986 795b88cb 2023-07-10 thomas * to apply unveil after the log message has been written during
7987 795b88cb 2023-07-10 thomas * the callback.
7988 795b88cb 2023-07-10 thomas */
7989 795b88cb 2023-07-10 thomas if (logmsg == NULL || strlen(logmsg) == 0)
7990 795b88cb 2023-07-10 thomas error = get_editor(&editor);
7991 795b88cb 2023-07-10 thomas else {
7992 795b88cb 2023-07-10 thomas error = got_dial_apply_unveil(proto);
7993 795b88cb 2023-07-10 thomas if (error)
7994 795b88cb 2023-07-10 thomas goto done;
7995 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
7996 795b88cb 2023-07-10 thomas got_worktree_get_root_path(worktree));
7997 795b88cb 2023-07-10 thomas }
7998 795b88cb 2023-07-10 thomas if (error)
7999 795b88cb 2023-07-10 thomas goto done;
8000 795b88cb 2023-07-10 thomas
8001 795b88cb 2023-07-10 thomas if (prepared_logmsg == NULL) {
8002 795b88cb 2023-07-10 thomas error = lookup_logmsg_ref(&merged_logmsg,
8003 795b88cb 2023-07-10 thomas argc > 0 ? &paths : NULL, &refs, worktree, repo);
8004 795b88cb 2023-07-10 thomas if (error)
8005 795b88cb 2023-07-10 thomas goto done;
8006 795b88cb 2023-07-10 thomas }
8007 795b88cb 2023-07-10 thomas
8008 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8009 795b88cb 2023-07-10 thomas if (error)
8010 795b88cb 2023-07-10 thomas goto done;
8011 795b88cb 2023-07-10 thomas
8012 795b88cb 2023-07-10 thomas cl_arg.editor = editor;
8013 795b88cb 2023-07-10 thomas cl_arg.cmdline_log = logmsg;
8014 795b88cb 2023-07-10 thomas cl_arg.prepared_log = prepared_logmsg;
8015 795b88cb 2023-07-10 thomas cl_arg.merged_log = merged_logmsg;
8016 795b88cb 2023-07-10 thomas cl_arg.non_interactive = non_interactive;
8017 795b88cb 2023-07-10 thomas cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8018 795b88cb 2023-07-10 thomas cl_arg.repo_path = got_repo_get_path(repo);
8019 795b88cb 2023-07-10 thomas cl_arg.dial_proto = proto;
8020 795b88cb 2023-07-10 thomas error = got_worktree_cvg_commit(&id, worktree, &paths, author,
8021 795b88cb 2023-07-10 thomas committer, allow_bad_symlinks, show_diff, commit_conflicts,
8022 795b88cb 2023-07-10 thomas collect_commit_logmsg, &cl_arg, print_status, NULL, proto, host,
8023 795b88cb 2023-07-10 thomas port, server_path, verbosity, remote, check_cancelled, repo);
8024 795b88cb 2023-07-10 thomas if (error) {
8025 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8026 795b88cb 2023-07-10 thomas cl_arg.logmsg_path != NULL)
8027 795b88cb 2023-07-10 thomas preserve_logmsg = 1;
8028 795b88cb 2023-07-10 thomas goto done;
8029 795b88cb 2023-07-10 thomas }
8030 795b88cb 2023-07-10 thomas
8031 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str, id);
8032 795b88cb 2023-07-10 thomas if (error)
8033 795b88cb 2023-07-10 thomas goto done;
8034 795b88cb 2023-07-10 thomas printf("Created commit %s\n", id_str);
8035 795b88cb 2023-07-10 thomas
8036 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8037 795b88cb 2023-07-10 thomas error = got_ref_delete(re->ref, repo);
8038 795b88cb 2023-07-10 thomas if (error)
8039 795b88cb 2023-07-10 thomas goto done;
8040 795b88cb 2023-07-10 thomas }
8041 795b88cb 2023-07-10 thomas
8042 795b88cb 2023-07-10 thomas done:
8043 795b88cb 2023-07-10 thomas if (preserve_logmsg) {
8044 795b88cb 2023-07-10 thomas fprintf(stderr, "%s: log message preserved in %s\n",
8045 795b88cb 2023-07-10 thomas getprogname(), cl_arg.logmsg_path);
8046 795b88cb 2023-07-10 thomas } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8047 795b88cb 2023-07-10 thomas error == NULL)
8048 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8049 795b88cb 2023-07-10 thomas free(cl_arg.logmsg_path);
8050 795b88cb 2023-07-10 thomas if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
8051 795b88cb 2023-07-10 thomas error = got_error_from_errno2("unlink", merged_logmsg);
8052 795b88cb 2023-07-10 thomas free(merged_logmsg);
8053 795b88cb 2023-07-10 thomas if (repo) {
8054 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8055 795b88cb 2023-07-10 thomas if (error == NULL)
8056 795b88cb 2023-07-10 thomas error = close_err;
8057 795b88cb 2023-07-10 thomas }
8058 795b88cb 2023-07-10 thomas if (worktree)
8059 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8060 795b88cb 2023-07-10 thomas if (pack_fds) {
8061 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8062 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8063 795b88cb 2023-07-10 thomas if (error == NULL)
8064 795b88cb 2023-07-10 thomas error = pack_err;
8065 795b88cb 2023-07-10 thomas }
8066 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8067 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8068 795b88cb 2023-07-10 thomas free(cwd);
8069 795b88cb 2023-07-10 thomas free(id_str);
8070 795b88cb 2023-07-10 thomas free(gitconfig_path);
8071 795b88cb 2023-07-10 thomas free(editor);
8072 795b88cb 2023-07-10 thomas free(committer);
8073 795b88cb 2023-07-10 thomas free(prepared_logmsg);
8074 795b88cb 2023-07-10 thomas return error;
8075 795b88cb 2023-07-10 thomas }
8076 795b88cb 2023-07-10 thomas
8077 795b88cb 2023-07-10 thomas /*
8078 795b88cb 2023-07-10 thomas * Print and if delete is set delete all ref_prefix references.
8079 795b88cb 2023-07-10 thomas * If wanted_ref is not NULL, only print or delete this reference.
8080 795b88cb 2023-07-10 thomas */
8081 795b88cb 2023-07-10 thomas static const struct got_error *
8082 795b88cb 2023-07-10 thomas process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
8083 795b88cb 2023-07-10 thomas const char *wanted_ref, int delete, struct got_worktree *worktree,
8084 795b88cb 2023-07-10 thomas struct got_repository *repo)
8085 795b88cb 2023-07-10 thomas {
8086 795b88cb 2023-07-10 thomas const struct got_error *err;
8087 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
8088 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
8089 795b88cb 2023-07-10 thomas struct got_reflist_entry *re;
8090 795b88cb 2023-07-10 thomas struct got_reflist_object_id_map *refs_idmap = NULL;
8091 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8092 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL;
8093 795b88cb 2023-07-10 thomas const char *header_prefix;
8094 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
8095 795b88cb 2023-07-10 thomas int found = 0;
8096 795b88cb 2023-07-10 thomas
8097 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
8098 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
8099 795b88cb 2023-07-10 thomas
8100 795b88cb 2023-07-10 thomas err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
8101 795b88cb 2023-07-10 thomas if (err)
8102 795b88cb 2023-07-10 thomas goto done;
8103 795b88cb 2023-07-10 thomas
8104 795b88cb 2023-07-10 thomas err = got_reflist_object_id_map_create(&refs_idmap, &refs, 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 if (worktree != NULL) {
8109 795b88cb 2023-07-10 thomas err = got_worktree_get_uuid(&uuidstr, worktree);
8110 795b88cb 2023-07-10 thomas if (err)
8111 795b88cb 2023-07-10 thomas goto done;
8112 795b88cb 2023-07-10 thomas }
8113 795b88cb 2023-07-10 thomas
8114 795b88cb 2023-07-10 thomas if (wanted_ref) {
8115 795b88cb 2023-07-10 thomas if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
8116 795b88cb 2023-07-10 thomas wanted_ref += 11;
8117 795b88cb 2023-07-10 thomas }
8118 795b88cb 2023-07-10 thomas
8119 795b88cb 2023-07-10 thomas if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
8120 795b88cb 2023-07-10 thomas header_prefix = "backout";
8121 795b88cb 2023-07-10 thomas else
8122 795b88cb 2023-07-10 thomas header_prefix = "cherrypick";
8123 795b88cb 2023-07-10 thomas
8124 795b88cb 2023-07-10 thomas TAILQ_FOREACH(re, &refs, entry) {
8125 795b88cb 2023-07-10 thomas const char *refname, *wt;
8126 795b88cb 2023-07-10 thomas
8127 795b88cb 2023-07-10 thomas refname = got_ref_get_name(re->ref);
8128 795b88cb 2023-07-10 thomas
8129 795b88cb 2023-07-10 thomas err = check_cancelled(NULL);
8130 795b88cb 2023-07-10 thomas if (err)
8131 795b88cb 2023-07-10 thomas goto done;
8132 795b88cb 2023-07-10 thomas
8133 795b88cb 2023-07-10 thomas if (strncmp(refname, ref_prefix, prefix_len) == 0)
8134 795b88cb 2023-07-10 thomas refname += prefix_len + 1; /* skip '-' delimiter */
8135 795b88cb 2023-07-10 thomas else
8136 795b88cb 2023-07-10 thomas continue;
8137 795b88cb 2023-07-10 thomas
8138 795b88cb 2023-07-10 thomas wt = refname;
8139 795b88cb 2023-07-10 thomas
8140 795b88cb 2023-07-10 thomas if (worktree == NULL || strncmp(refname, uuidstr,
8141 795b88cb 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN) == 0)
8142 795b88cb 2023-07-10 thomas refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8143 795b88cb 2023-07-10 thomas else
8144 795b88cb 2023-07-10 thomas continue;
8145 795b88cb 2023-07-10 thomas
8146 795b88cb 2023-07-10 thomas err = got_repo_match_object_id(&id, NULL, refname,
8147 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8148 795b88cb 2023-07-10 thomas if (err)
8149 795b88cb 2023-07-10 thomas goto done;
8150 795b88cb 2023-07-10 thomas
8151 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
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 if (wanted_ref)
8156 795b88cb 2023-07-10 thomas found = strncmp(wanted_ref, refname,
8157 795b88cb 2023-07-10 thomas strlen(wanted_ref)) == 0;
8158 795b88cb 2023-07-10 thomas if (wanted_ref && !found) {
8159 795b88cb 2023-07-10 thomas struct got_reflist_head *ci_refs;
8160 795b88cb 2023-07-10 thomas
8161 795b88cb 2023-07-10 thomas ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
8162 795b88cb 2023-07-10 thomas id);
8163 795b88cb 2023-07-10 thomas
8164 795b88cb 2023-07-10 thomas if (ci_refs) {
8165 795b88cb 2023-07-10 thomas char *refs_str = NULL;
8166 795b88cb 2023-07-10 thomas char const *r = NULL;
8167 795b88cb 2023-07-10 thomas
8168 795b88cb 2023-07-10 thomas err = build_refs_str(&refs_str, ci_refs, id,
8169 795b88cb 2023-07-10 thomas repo, 1);
8170 795b88cb 2023-07-10 thomas if (err)
8171 795b88cb 2023-07-10 thomas goto done;
8172 795b88cb 2023-07-10 thomas
8173 795b88cb 2023-07-10 thomas r = refs_str;
8174 795b88cb 2023-07-10 thomas while (r) {
8175 795b88cb 2023-07-10 thomas if (strncmp(r, wanted_ref,
8176 795b88cb 2023-07-10 thomas strlen(wanted_ref)) == 0) {
8177 795b88cb 2023-07-10 thomas found = 1;
8178 795b88cb 2023-07-10 thomas break;
8179 795b88cb 2023-07-10 thomas }
8180 795b88cb 2023-07-10 thomas r = strchr(r, ' ');
8181 795b88cb 2023-07-10 thomas if (r)
8182 795b88cb 2023-07-10 thomas ++r;
8183 795b88cb 2023-07-10 thomas }
8184 795b88cb 2023-07-10 thomas free(refs_str);
8185 795b88cb 2023-07-10 thomas }
8186 795b88cb 2023-07-10 thomas }
8187 795b88cb 2023-07-10 thomas
8188 795b88cb 2023-07-10 thomas if (wanted_ref == NULL || found) {
8189 795b88cb 2023-07-10 thomas if (delete) {
8190 795b88cb 2023-07-10 thomas err = got_ref_delete(re->ref, repo);
8191 795b88cb 2023-07-10 thomas if (err)
8192 795b88cb 2023-07-10 thomas goto done;
8193 795b88cb 2023-07-10 thomas printf("Deleted: ");
8194 795b88cb 2023-07-10 thomas err = print_commit_oneline(commit, id, repo,
8195 795b88cb 2023-07-10 thomas refs_idmap);
8196 795b88cb 2023-07-10 thomas } else {
8197 795b88cb 2023-07-10 thomas /*
8198 795b88cb 2023-07-10 thomas * Print paths modified by commit to help
8199 795b88cb 2023-07-10 thomas * associate commits with worktree changes.
8200 795b88cb 2023-07-10 thomas */
8201 795b88cb 2023-07-10 thomas err = get_changed_paths(&paths, commit,
8202 795b88cb 2023-07-10 thomas repo, NULL);
8203 795b88cb 2023-07-10 thomas if (err)
8204 795b88cb 2023-07-10 thomas goto done;
8205 795b88cb 2023-07-10 thomas
8206 795b88cb 2023-07-10 thomas err = print_commit(commit, id, repo, NULL,
8207 795b88cb 2023-07-10 thomas &paths, NULL, 0, 0, refs_idmap, NULL,
8208 795b88cb 2023-07-10 thomas header_prefix);
8209 795b88cb 2023-07-10 thomas got_pathlist_free(&paths,
8210 795b88cb 2023-07-10 thomas GOT_PATHLIST_FREE_ALL);
8211 795b88cb 2023-07-10 thomas
8212 795b88cb 2023-07-10 thomas if (worktree == NULL)
8213 795b88cb 2023-07-10 thomas printf("work tree: %.*s\n\n",
8214 795b88cb 2023-07-10 thomas GOT_WORKTREE_UUID_STRLEN, wt);
8215 795b88cb 2023-07-10 thomas }
8216 795b88cb 2023-07-10 thomas if (err || found)
8217 795b88cb 2023-07-10 thomas goto done;
8218 795b88cb 2023-07-10 thomas }
8219 795b88cb 2023-07-10 thomas
8220 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8221 795b88cb 2023-07-10 thomas commit = NULL;
8222 795b88cb 2023-07-10 thomas free(id);
8223 795b88cb 2023-07-10 thomas id = NULL;
8224 795b88cb 2023-07-10 thomas }
8225 795b88cb 2023-07-10 thomas
8226 795b88cb 2023-07-10 thomas if (wanted_ref != NULL && !found)
8227 795b88cb 2023-07-10 thomas err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
8228 795b88cb 2023-07-10 thomas
8229 795b88cb 2023-07-10 thomas done:
8230 795b88cb 2023-07-10 thomas free(id);
8231 795b88cb 2023-07-10 thomas free(uuidstr);
8232 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8233 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8234 795b88cb 2023-07-10 thomas if (refs_idmap)
8235 795b88cb 2023-07-10 thomas got_reflist_object_id_map_free(refs_idmap);
8236 795b88cb 2023-07-10 thomas if (commit)
8237 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8238 795b88cb 2023-07-10 thomas return err;
8239 795b88cb 2023-07-10 thomas }
8240 795b88cb 2023-07-10 thomas
8241 795b88cb 2023-07-10 thomas /*
8242 795b88cb 2023-07-10 thomas * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
8243 795b88cb 2023-07-10 thomas * identified by id for log messages to prepopulate the editor on commit.
8244 795b88cb 2023-07-10 thomas */
8245 795b88cb 2023-07-10 thomas static const struct got_error *
8246 795b88cb 2023-07-10 thomas logmsg_ref(struct got_object_id *id, const char *prefix,
8247 795b88cb 2023-07-10 thomas struct got_worktree *worktree, struct got_repository *repo)
8248 795b88cb 2023-07-10 thomas {
8249 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
8250 795b88cb 2023-07-10 thomas char *idstr, *ref = NULL, *refname = NULL;
8251 795b88cb 2023-07-10 thomas int histedit_in_progress;
8252 795b88cb 2023-07-10 thomas int rebase_in_progress, merge_in_progress;
8253 795b88cb 2023-07-10 thomas
8254 795b88cb 2023-07-10 thomas /*
8255 795b88cb 2023-07-10 thomas * Silenty refuse to create merge reference if any histedit, merge,
8256 795b88cb 2023-07-10 thomas * or rebase operation is in progress.
8257 795b88cb 2023-07-10 thomas */
8258 795b88cb 2023-07-10 thomas err = got_worktree_histedit_in_progress(&histedit_in_progress,
8259 795b88cb 2023-07-10 thomas worktree);
8260 795b88cb 2023-07-10 thomas if (err)
8261 795b88cb 2023-07-10 thomas return err;
8262 795b88cb 2023-07-10 thomas if (histedit_in_progress)
8263 795b88cb 2023-07-10 thomas return NULL;
8264 795b88cb 2023-07-10 thomas
8265 795b88cb 2023-07-10 thomas err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8266 795b88cb 2023-07-10 thomas if (err)
8267 795b88cb 2023-07-10 thomas return err;
8268 795b88cb 2023-07-10 thomas if (rebase_in_progress)
8269 795b88cb 2023-07-10 thomas return NULL;
8270 795b88cb 2023-07-10 thomas
8271 795b88cb 2023-07-10 thomas err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
8272 795b88cb 2023-07-10 thomas repo);
8273 795b88cb 2023-07-10 thomas if (err)
8274 795b88cb 2023-07-10 thomas return err;
8275 795b88cb 2023-07-10 thomas if (merge_in_progress)
8276 795b88cb 2023-07-10 thomas return NULL;
8277 795b88cb 2023-07-10 thomas
8278 795b88cb 2023-07-10 thomas err = got_object_id_str(&idstr, id);
8279 795b88cb 2023-07-10 thomas if (err)
8280 795b88cb 2023-07-10 thomas return err;
8281 795b88cb 2023-07-10 thomas
8282 795b88cb 2023-07-10 thomas err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
8283 795b88cb 2023-07-10 thomas if (err)
8284 795b88cb 2023-07-10 thomas goto done;
8285 795b88cb 2023-07-10 thomas
8286 795b88cb 2023-07-10 thomas if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
8287 795b88cb 2023-07-10 thomas err = got_error_from_errno("asprintf");
8288 795b88cb 2023-07-10 thomas goto done;
8289 795b88cb 2023-07-10 thomas }
8290 795b88cb 2023-07-10 thomas
8291 795b88cb 2023-07-10 thomas err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
8292 795b88cb 2023-07-10 thomas -1, repo);
8293 795b88cb 2023-07-10 thomas done:
8294 795b88cb 2023-07-10 thomas free(ref);
8295 795b88cb 2023-07-10 thomas free(idstr);
8296 795b88cb 2023-07-10 thomas free(refname);
8297 795b88cb 2023-07-10 thomas return err;
8298 795b88cb 2023-07-10 thomas }
8299 795b88cb 2023-07-10 thomas
8300 795b88cb 2023-07-10 thomas __dead static void
8301 795b88cb 2023-07-10 thomas usage_cherrypick(void)
8302 795b88cb 2023-07-10 thomas {
8303 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
8304 795b88cb 2023-07-10 thomas getprogname());
8305 795b88cb 2023-07-10 thomas exit(1);
8306 795b88cb 2023-07-10 thomas }
8307 795b88cb 2023-07-10 thomas
8308 795b88cb 2023-07-10 thomas static const struct got_error *
8309 795b88cb 2023-07-10 thomas cmd_cherrypick(int argc, char *argv[])
8310 795b88cb 2023-07-10 thomas {
8311 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
8312 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8313 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8314 795b88cb 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
8315 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8316 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8317 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8318 795b88cb 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
8319 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
8320 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8321 795b88cb 2023-07-10 thomas
8322 795b88cb 2023-07-10 thomas #ifndef PROFILE
8323 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8324 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
8325 795b88cb 2023-07-10 thomas err(1, "pledge");
8326 795b88cb 2023-07-10 thomas #endif
8327 795b88cb 2023-07-10 thomas
8328 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
8329 795b88cb 2023-07-10 thomas switch (ch) {
8330 795b88cb 2023-07-10 thomas case 'l':
8331 795b88cb 2023-07-10 thomas list_refs = 1;
8332 795b88cb 2023-07-10 thomas break;
8333 795b88cb 2023-07-10 thomas case 'X':
8334 795b88cb 2023-07-10 thomas remove_refs = 1;
8335 795b88cb 2023-07-10 thomas break;
8336 795b88cb 2023-07-10 thomas default:
8337 795b88cb 2023-07-10 thomas usage_cherrypick();
8338 795b88cb 2023-07-10 thomas /* NOTREACHED */
8339 795b88cb 2023-07-10 thomas }
8340 795b88cb 2023-07-10 thomas }
8341 795b88cb 2023-07-10 thomas
8342 795b88cb 2023-07-10 thomas argc -= optind;
8343 795b88cb 2023-07-10 thomas argv += optind;
8344 795b88cb 2023-07-10 thomas
8345 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8346 795b88cb 2023-07-10 thomas if (argc != 0 && argc != 1)
8347 795b88cb 2023-07-10 thomas usage_cherrypick();
8348 795b88cb 2023-07-10 thomas } else if (argc != 1)
8349 795b88cb 2023-07-10 thomas usage_cherrypick();
8350 795b88cb 2023-07-10 thomas if (list_refs && remove_refs)
8351 795b88cb 2023-07-10 thomas option_conflict('l', 'X');
8352 795b88cb 2023-07-10 thomas
8353 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8354 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8355 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8356 795b88cb 2023-07-10 thomas goto done;
8357 795b88cb 2023-07-10 thomas }
8358 795b88cb 2023-07-10 thomas
8359 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8360 795b88cb 2023-07-10 thomas if (error != NULL)
8361 795b88cb 2023-07-10 thomas goto done;
8362 795b88cb 2023-07-10 thomas
8363 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8364 795b88cb 2023-07-10 thomas if (error) {
8365 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8366 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
8367 795b88cb 2023-07-10 thomas goto done;
8368 795b88cb 2023-07-10 thomas } else {
8369 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8370 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error,
8371 795b88cb 2023-07-10 thomas "cherrypick", cwd);
8372 795b88cb 2023-07-10 thomas goto done;
8373 795b88cb 2023-07-10 thomas }
8374 795b88cb 2023-07-10 thomas }
8375 795b88cb 2023-07-10 thomas
8376 795b88cb 2023-07-10 thomas error = got_repo_open(&repo,
8377 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
8378 795b88cb 2023-07-10 thomas NULL, pack_fds);
8379 795b88cb 2023-07-10 thomas if (error != NULL)
8380 795b88cb 2023-07-10 thomas goto done;
8381 795b88cb 2023-07-10 thomas
8382 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8383 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
8384 795b88cb 2023-07-10 thomas if (error)
8385 795b88cb 2023-07-10 thomas goto done;
8386 795b88cb 2023-07-10 thomas
8387 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8388 795b88cb 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8389 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
8390 795b88cb 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8391 795b88cb 2023-07-10 thomas goto done;
8392 795b88cb 2023-07-10 thomas }
8393 795b88cb 2023-07-10 thomas
8394 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8395 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8396 795b88cb 2023-07-10 thomas if (error)
8397 795b88cb 2023-07-10 thomas goto done;
8398 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
8399 795b88cb 2023-07-10 thomas if (error)
8400 795b88cb 2023-07-10 thomas goto done;
8401 795b88cb 2023-07-10 thomas
8402 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8403 795b88cb 2023-07-10 thomas if (error)
8404 795b88cb 2023-07-10 thomas goto done;
8405 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8406 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
8407 795b88cb 2023-07-10 thomas error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
8408 795b88cb 2023-07-10 thomas commit_id, repo, update_progress, &upa, check_cancelled,
8409 795b88cb 2023-07-10 thomas NULL);
8410 795b88cb 2023-07-10 thomas if (error != NULL)
8411 795b88cb 2023-07-10 thomas goto done;
8412 795b88cb 2023-07-10 thomas
8413 795b88cb 2023-07-10 thomas if (upa.did_something) {
8414 795b88cb 2023-07-10 thomas error = logmsg_ref(commit_id,
8415 795b88cb 2023-07-10 thomas GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
8416 795b88cb 2023-07-10 thomas if (error)
8417 795b88cb 2023-07-10 thomas goto done;
8418 795b88cb 2023-07-10 thomas printf("Merged commit %s\n", commit_id_str);
8419 795b88cb 2023-07-10 thomas }
8420 795b88cb 2023-07-10 thomas print_merge_progress_stats(&upa);
8421 795b88cb 2023-07-10 thomas done:
8422 795b88cb 2023-07-10 thomas free(cwd);
8423 795b88cb 2023-07-10 thomas if (commit)
8424 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8425 795b88cb 2023-07-10 thomas free(commit_id_str);
8426 795b88cb 2023-07-10 thomas if (worktree)
8427 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8428 795b88cb 2023-07-10 thomas if (repo) {
8429 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8430 795b88cb 2023-07-10 thomas if (error == NULL)
8431 795b88cb 2023-07-10 thomas error = close_err;
8432 795b88cb 2023-07-10 thomas }
8433 795b88cb 2023-07-10 thomas if (pack_fds) {
8434 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8435 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8436 795b88cb 2023-07-10 thomas if (error == NULL)
8437 795b88cb 2023-07-10 thomas error = pack_err;
8438 795b88cb 2023-07-10 thomas }
8439 795b88cb 2023-07-10 thomas
8440 795b88cb 2023-07-10 thomas return error;
8441 795b88cb 2023-07-10 thomas }
8442 795b88cb 2023-07-10 thomas
8443 795b88cb 2023-07-10 thomas __dead static void
8444 795b88cb 2023-07-10 thomas usage_backout(void)
8445 795b88cb 2023-07-10 thomas {
8446 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
8447 795b88cb 2023-07-10 thomas exit(1);
8448 795b88cb 2023-07-10 thomas }
8449 795b88cb 2023-07-10 thomas
8450 795b88cb 2023-07-10 thomas static const struct got_error *
8451 795b88cb 2023-07-10 thomas cmd_backout(int argc, char *argv[])
8452 795b88cb 2023-07-10 thomas {
8453 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
8454 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8455 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8456 795b88cb 2023-07-10 thomas char *cwd = NULL, *commit_id_str = NULL;
8457 795b88cb 2023-07-10 thomas struct got_object_id *commit_id = NULL;
8458 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8459 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8460 795b88cb 2023-07-10 thomas int ch, list_refs = 0, remove_refs = 0;
8461 795b88cb 2023-07-10 thomas struct got_update_progress_arg upa;
8462 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8463 795b88cb 2023-07-10 thomas
8464 795b88cb 2023-07-10 thomas #ifndef PROFILE
8465 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8466 795b88cb 2023-07-10 thomas "unveil", NULL) == -1)
8467 795b88cb 2023-07-10 thomas err(1, "pledge");
8468 795b88cb 2023-07-10 thomas #endif
8469 795b88cb 2023-07-10 thomas
8470 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "lX")) != -1) {
8471 795b88cb 2023-07-10 thomas switch (ch) {
8472 795b88cb 2023-07-10 thomas case 'l':
8473 795b88cb 2023-07-10 thomas list_refs = 1;
8474 795b88cb 2023-07-10 thomas break;
8475 795b88cb 2023-07-10 thomas case 'X':
8476 795b88cb 2023-07-10 thomas remove_refs = 1;
8477 795b88cb 2023-07-10 thomas break;
8478 795b88cb 2023-07-10 thomas default:
8479 795b88cb 2023-07-10 thomas usage_backout();
8480 795b88cb 2023-07-10 thomas /* NOTREACHED */
8481 795b88cb 2023-07-10 thomas }
8482 795b88cb 2023-07-10 thomas }
8483 795b88cb 2023-07-10 thomas
8484 795b88cb 2023-07-10 thomas argc -= optind;
8485 795b88cb 2023-07-10 thomas argv += optind;
8486 795b88cb 2023-07-10 thomas
8487 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8488 795b88cb 2023-07-10 thomas if (argc != 0 && argc != 1)
8489 795b88cb 2023-07-10 thomas usage_backout();
8490 795b88cb 2023-07-10 thomas } else if (argc != 1)
8491 795b88cb 2023-07-10 thomas usage_backout();
8492 795b88cb 2023-07-10 thomas if (list_refs && remove_refs)
8493 795b88cb 2023-07-10 thomas option_conflict('l', 'X');
8494 795b88cb 2023-07-10 thomas
8495 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8496 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8497 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8498 795b88cb 2023-07-10 thomas goto done;
8499 795b88cb 2023-07-10 thomas }
8500 795b88cb 2023-07-10 thomas
8501 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8502 795b88cb 2023-07-10 thomas if (error != NULL)
8503 795b88cb 2023-07-10 thomas goto done;
8504 795b88cb 2023-07-10 thomas
8505 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8506 795b88cb 2023-07-10 thomas if (error) {
8507 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8508 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_NOT_WORKTREE)
8509 795b88cb 2023-07-10 thomas goto done;
8510 795b88cb 2023-07-10 thomas } else {
8511 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
8512 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error,
8513 795b88cb 2023-07-10 thomas "backout", cwd);
8514 795b88cb 2023-07-10 thomas goto done;
8515 795b88cb 2023-07-10 thomas }
8516 795b88cb 2023-07-10 thomas }
8517 795b88cb 2023-07-10 thomas
8518 795b88cb 2023-07-10 thomas error = got_repo_open(&repo,
8519 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_repo_path(worktree) : cwd,
8520 795b88cb 2023-07-10 thomas NULL, pack_fds);
8521 795b88cb 2023-07-10 thomas if (error != NULL)
8522 795b88cb 2023-07-10 thomas goto done;
8523 795b88cb 2023-07-10 thomas
8524 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 0,
8525 795b88cb 2023-07-10 thomas worktree ? got_worktree_get_root_path(worktree) : NULL);
8526 795b88cb 2023-07-10 thomas if (error)
8527 795b88cb 2023-07-10 thomas goto done;
8528 795b88cb 2023-07-10 thomas
8529 795b88cb 2023-07-10 thomas if (list_refs || remove_refs) {
8530 795b88cb 2023-07-10 thomas error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
8531 795b88cb 2023-07-10 thomas GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
8532 795b88cb 2023-07-10 thomas argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
8533 795b88cb 2023-07-10 thomas goto done;
8534 795b88cb 2023-07-10 thomas }
8535 795b88cb 2023-07-10 thomas
8536 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL, argv[0],
8537 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_COMMIT, NULL, repo);
8538 795b88cb 2023-07-10 thomas if (error)
8539 795b88cb 2023-07-10 thomas goto done;
8540 795b88cb 2023-07-10 thomas error = got_object_id_str(&commit_id_str, commit_id);
8541 795b88cb 2023-07-10 thomas if (error)
8542 795b88cb 2023-07-10 thomas goto done;
8543 795b88cb 2023-07-10 thomas
8544 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8545 795b88cb 2023-07-10 thomas if (error)
8546 795b88cb 2023-07-10 thomas goto done;
8547 795b88cb 2023-07-10 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8548 795b88cb 2023-07-10 thomas if (pid == NULL) {
8549 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_ROOT_COMMIT);
8550 795b88cb 2023-07-10 thomas goto done;
8551 795b88cb 2023-07-10 thomas }
8552 795b88cb 2023-07-10 thomas
8553 795b88cb 2023-07-10 thomas memset(&upa, 0, sizeof(upa));
8554 795b88cb 2023-07-10 thomas error = got_worktree_merge_files(worktree, commit_id, &pid->id,
8555 795b88cb 2023-07-10 thomas repo, update_progress, &upa, check_cancelled, NULL);
8556 795b88cb 2023-07-10 thomas if (error != NULL)
8557 795b88cb 2023-07-10 thomas goto done;
8558 795b88cb 2023-07-10 thomas
8559 795b88cb 2023-07-10 thomas if (upa.did_something) {
8560 795b88cb 2023-07-10 thomas error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8561 795b88cb 2023-07-10 thomas worktree, repo);
8562 795b88cb 2023-07-10 thomas if (error)
8563 795b88cb 2023-07-10 thomas goto done;
8564 795b88cb 2023-07-10 thomas printf("Backed out commit %s\n", commit_id_str);
8565 795b88cb 2023-07-10 thomas }
8566 795b88cb 2023-07-10 thomas print_merge_progress_stats(&upa);
8567 795b88cb 2023-07-10 thomas done:
8568 795b88cb 2023-07-10 thomas free(cwd);
8569 795b88cb 2023-07-10 thomas if (commit)
8570 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8571 795b88cb 2023-07-10 thomas free(commit_id_str);
8572 795b88cb 2023-07-10 thomas if (worktree)
8573 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8574 795b88cb 2023-07-10 thomas if (repo) {
8575 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8576 795b88cb 2023-07-10 thomas if (error == NULL)
8577 795b88cb 2023-07-10 thomas error = close_err;
8578 795b88cb 2023-07-10 thomas }
8579 795b88cb 2023-07-10 thomas if (pack_fds) {
8580 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8581 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8582 795b88cb 2023-07-10 thomas if (error == NULL)
8583 795b88cb 2023-07-10 thomas error = pack_err;
8584 795b88cb 2023-07-10 thomas }
8585 795b88cb 2023-07-10 thomas return error;
8586 795b88cb 2023-07-10 thomas }
8587 795b88cb 2023-07-10 thomas
8588 795b88cb 2023-07-10 thomas __dead static void
8589 795b88cb 2023-07-10 thomas usage_cat(void)
8590 795b88cb 2023-07-10 thomas {
8591 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
8592 795b88cb 2023-07-10 thomas "arg ...\n", getprogname());
8593 795b88cb 2023-07-10 thomas exit(1);
8594 795b88cb 2023-07-10 thomas }
8595 795b88cb 2023-07-10 thomas
8596 795b88cb 2023-07-10 thomas static const struct got_error *
8597 795b88cb 2023-07-10 thomas cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8598 795b88cb 2023-07-10 thomas {
8599 795b88cb 2023-07-10 thomas const struct got_error *err;
8600 795b88cb 2023-07-10 thomas struct got_blob_object *blob;
8601 795b88cb 2023-07-10 thomas int fd = -1;
8602 795b88cb 2023-07-10 thomas
8603 795b88cb 2023-07-10 thomas fd = got_opentempfd();
8604 795b88cb 2023-07-10 thomas if (fd == -1)
8605 795b88cb 2023-07-10 thomas return got_error_from_errno("got_opentempfd");
8606 795b88cb 2023-07-10 thomas
8607 795b88cb 2023-07-10 thomas err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
8608 795b88cb 2023-07-10 thomas if (err)
8609 795b88cb 2023-07-10 thomas goto done;
8610 795b88cb 2023-07-10 thomas
8611 795b88cb 2023-07-10 thomas err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8612 795b88cb 2023-07-10 thomas done:
8613 795b88cb 2023-07-10 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
8614 795b88cb 2023-07-10 thomas err = got_error_from_errno("close");
8615 795b88cb 2023-07-10 thomas if (blob)
8616 795b88cb 2023-07-10 thomas got_object_blob_close(blob);
8617 795b88cb 2023-07-10 thomas return err;
8618 795b88cb 2023-07-10 thomas }
8619 795b88cb 2023-07-10 thomas
8620 795b88cb 2023-07-10 thomas static const struct got_error *
8621 795b88cb 2023-07-10 thomas cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8622 795b88cb 2023-07-10 thomas {
8623 795b88cb 2023-07-10 thomas const struct got_error *err;
8624 795b88cb 2023-07-10 thomas struct got_tree_object *tree;
8625 795b88cb 2023-07-10 thomas int nentries, i;
8626 795b88cb 2023-07-10 thomas
8627 795b88cb 2023-07-10 thomas err = got_object_open_as_tree(&tree, repo, id);
8628 795b88cb 2023-07-10 thomas if (err)
8629 795b88cb 2023-07-10 thomas return err;
8630 795b88cb 2023-07-10 thomas
8631 795b88cb 2023-07-10 thomas nentries = got_object_tree_get_nentries(tree);
8632 795b88cb 2023-07-10 thomas for (i = 0; i < nentries; i++) {
8633 795b88cb 2023-07-10 thomas struct got_tree_entry *te;
8634 795b88cb 2023-07-10 thomas char *id_str;
8635 795b88cb 2023-07-10 thomas if (sigint_received || sigpipe_received)
8636 795b88cb 2023-07-10 thomas break;
8637 795b88cb 2023-07-10 thomas te = got_object_tree_get_entry(tree, i);
8638 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8639 795b88cb 2023-07-10 thomas if (err)
8640 795b88cb 2023-07-10 thomas break;
8641 795b88cb 2023-07-10 thomas fprintf(outfile, "%s %.7o %s\n", id_str,
8642 795b88cb 2023-07-10 thomas got_tree_entry_get_mode(te),
8643 795b88cb 2023-07-10 thomas got_tree_entry_get_name(te));
8644 795b88cb 2023-07-10 thomas free(id_str);
8645 795b88cb 2023-07-10 thomas }
8646 795b88cb 2023-07-10 thomas
8647 795b88cb 2023-07-10 thomas got_object_tree_close(tree);
8648 795b88cb 2023-07-10 thomas return err;
8649 795b88cb 2023-07-10 thomas }
8650 795b88cb 2023-07-10 thomas
8651 795b88cb 2023-07-10 thomas static const struct got_error *
8652 795b88cb 2023-07-10 thomas cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8653 795b88cb 2023-07-10 thomas {
8654 795b88cb 2023-07-10 thomas const struct got_error *err;
8655 795b88cb 2023-07-10 thomas struct got_commit_object *commit;
8656 795b88cb 2023-07-10 thomas const struct got_object_id_queue *parent_ids;
8657 795b88cb 2023-07-10 thomas struct got_object_qid *pid;
8658 795b88cb 2023-07-10 thomas char *id_str = NULL;
8659 795b88cb 2023-07-10 thomas const char *logmsg = NULL;
8660 795b88cb 2023-07-10 thomas char gmtoff[6];
8661 795b88cb 2023-07-10 thomas
8662 795b88cb 2023-07-10 thomas err = got_object_open_as_commit(&commit, repo, id);
8663 795b88cb 2023-07-10 thomas if (err)
8664 795b88cb 2023-07-10 thomas return err;
8665 795b88cb 2023-07-10 thomas
8666 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8667 795b88cb 2023-07-10 thomas if (err)
8668 795b88cb 2023-07-10 thomas goto done;
8669 795b88cb 2023-07-10 thomas
8670 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8671 795b88cb 2023-07-10 thomas parent_ids = got_object_commit_get_parent_ids(commit);
8672 795b88cb 2023-07-10 thomas fprintf(outfile, "numparents %d\n",
8673 795b88cb 2023-07-10 thomas got_object_commit_get_nparents(commit));
8674 795b88cb 2023-07-10 thomas STAILQ_FOREACH(pid, parent_ids, entry) {
8675 795b88cb 2023-07-10 thomas char *pid_str;
8676 795b88cb 2023-07-10 thomas err = got_object_id_str(&pid_str, &pid->id);
8677 795b88cb 2023-07-10 thomas if (err)
8678 795b88cb 2023-07-10 thomas goto done;
8679 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8680 795b88cb 2023-07-10 thomas free(pid_str);
8681 795b88cb 2023-07-10 thomas }
8682 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8683 795b88cb 2023-07-10 thomas got_object_commit_get_author_gmtoff(commit));
8684 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
8685 795b88cb 2023-07-10 thomas got_object_commit_get_author(commit),
8686 795b88cb 2023-07-10 thomas (long long)got_object_commit_get_author_time(commit),
8687 795b88cb 2023-07-10 thomas gmtoff);
8688 795b88cb 2023-07-10 thomas
8689 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8690 795b88cb 2023-07-10 thomas got_object_commit_get_committer_gmtoff(commit));
8691 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
8692 795b88cb 2023-07-10 thomas got_object_commit_get_committer(commit),
8693 795b88cb 2023-07-10 thomas (long long)got_object_commit_get_committer_time(commit),
8694 795b88cb 2023-07-10 thomas gmtoff);
8695 795b88cb 2023-07-10 thomas
8696 795b88cb 2023-07-10 thomas logmsg = got_object_commit_get_logmsg_raw(commit);
8697 795b88cb 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8698 795b88cb 2023-07-10 thomas fprintf(outfile, "%s", logmsg);
8699 795b88cb 2023-07-10 thomas done:
8700 795b88cb 2023-07-10 thomas free(id_str);
8701 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8702 795b88cb 2023-07-10 thomas return err;
8703 795b88cb 2023-07-10 thomas }
8704 795b88cb 2023-07-10 thomas
8705 795b88cb 2023-07-10 thomas static const struct got_error *
8706 795b88cb 2023-07-10 thomas cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8707 795b88cb 2023-07-10 thomas {
8708 795b88cb 2023-07-10 thomas const struct got_error *err;
8709 795b88cb 2023-07-10 thomas struct got_tag_object *tag;
8710 795b88cb 2023-07-10 thomas char *id_str = NULL;
8711 795b88cb 2023-07-10 thomas const char *tagmsg = NULL;
8712 795b88cb 2023-07-10 thomas char gmtoff[6];
8713 795b88cb 2023-07-10 thomas
8714 795b88cb 2023-07-10 thomas err = got_object_open_as_tag(&tag, repo, id);
8715 795b88cb 2023-07-10 thomas if (err)
8716 795b88cb 2023-07-10 thomas return err;
8717 795b88cb 2023-07-10 thomas
8718 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8719 795b88cb 2023-07-10 thomas if (err)
8720 795b88cb 2023-07-10 thomas goto done;
8721 795b88cb 2023-07-10 thomas
8722 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8723 795b88cb 2023-07-10 thomas
8724 795b88cb 2023-07-10 thomas switch (got_object_tag_get_object_type(tag)) {
8725 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
8726 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8727 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_BLOB);
8728 795b88cb 2023-07-10 thomas break;
8729 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
8730 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8731 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_TREE);
8732 795b88cb 2023-07-10 thomas break;
8733 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
8734 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8735 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_COMMIT);
8736 795b88cb 2023-07-10 thomas break;
8737 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
8738 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8739 795b88cb 2023-07-10 thomas GOT_OBJ_LABEL_TAG);
8740 795b88cb 2023-07-10 thomas break;
8741 795b88cb 2023-07-10 thomas default:
8742 795b88cb 2023-07-10 thomas break;
8743 795b88cb 2023-07-10 thomas }
8744 795b88cb 2023-07-10 thomas
8745 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8746 795b88cb 2023-07-10 thomas got_object_tag_get_name(tag));
8747 795b88cb 2023-07-10 thomas
8748 795b88cb 2023-07-10 thomas got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
8749 795b88cb 2023-07-10 thomas got_object_tag_get_tagger_gmtoff(tag));
8750 795b88cb 2023-07-10 thomas fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
8751 795b88cb 2023-07-10 thomas got_object_tag_get_tagger(tag),
8752 795b88cb 2023-07-10 thomas (long long)got_object_tag_get_tagger_time(tag),
8753 795b88cb 2023-07-10 thomas gmtoff);
8754 795b88cb 2023-07-10 thomas
8755 795b88cb 2023-07-10 thomas tagmsg = got_object_tag_get_message(tag);
8756 795b88cb 2023-07-10 thomas fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8757 795b88cb 2023-07-10 thomas fprintf(outfile, "%s", tagmsg);
8758 795b88cb 2023-07-10 thomas done:
8759 795b88cb 2023-07-10 thomas free(id_str);
8760 795b88cb 2023-07-10 thomas got_object_tag_close(tag);
8761 795b88cb 2023-07-10 thomas return err;
8762 795b88cb 2023-07-10 thomas }
8763 795b88cb 2023-07-10 thomas
8764 795b88cb 2023-07-10 thomas static const struct got_error *
8765 795b88cb 2023-07-10 thomas cmd_cat(int argc, char *argv[])
8766 795b88cb 2023-07-10 thomas {
8767 795b88cb 2023-07-10 thomas const struct got_error *error;
8768 795b88cb 2023-07-10 thomas struct got_repository *repo = NULL;
8769 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
8770 795b88cb 2023-07-10 thomas char *cwd = NULL, *repo_path = NULL, *label = NULL;
8771 795b88cb 2023-07-10 thomas const char *commit_id_str = NULL;
8772 795b88cb 2023-07-10 thomas struct got_object_id *id = NULL, *commit_id = NULL;
8773 795b88cb 2023-07-10 thomas struct got_commit_object *commit = NULL;
8774 795b88cb 2023-07-10 thomas int ch, obj_type, i, force_path = 0;
8775 795b88cb 2023-07-10 thomas struct got_reflist_head refs;
8776 795b88cb 2023-07-10 thomas int *pack_fds = NULL;
8777 795b88cb 2023-07-10 thomas
8778 795b88cb 2023-07-10 thomas TAILQ_INIT(&refs);
8779 795b88cb 2023-07-10 thomas
8780 795b88cb 2023-07-10 thomas #ifndef PROFILE
8781 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8782 795b88cb 2023-07-10 thomas NULL) == -1)
8783 795b88cb 2023-07-10 thomas err(1, "pledge");
8784 795b88cb 2023-07-10 thomas #endif
8785 795b88cb 2023-07-10 thomas
8786 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
8787 795b88cb 2023-07-10 thomas switch (ch) {
8788 795b88cb 2023-07-10 thomas case 'c':
8789 795b88cb 2023-07-10 thomas commit_id_str = optarg;
8790 795b88cb 2023-07-10 thomas break;
8791 795b88cb 2023-07-10 thomas case 'P':
8792 795b88cb 2023-07-10 thomas force_path = 1;
8793 795b88cb 2023-07-10 thomas break;
8794 795b88cb 2023-07-10 thomas case 'r':
8795 795b88cb 2023-07-10 thomas repo_path = realpath(optarg, NULL);
8796 795b88cb 2023-07-10 thomas if (repo_path == NULL)
8797 795b88cb 2023-07-10 thomas return got_error_from_errno2("realpath",
8798 795b88cb 2023-07-10 thomas optarg);
8799 795b88cb 2023-07-10 thomas got_path_strip_trailing_slashes(repo_path);
8800 795b88cb 2023-07-10 thomas break;
8801 795b88cb 2023-07-10 thomas default:
8802 795b88cb 2023-07-10 thomas usage_cat();
8803 795b88cb 2023-07-10 thomas /* NOTREACHED */
8804 795b88cb 2023-07-10 thomas }
8805 795b88cb 2023-07-10 thomas }
8806 795b88cb 2023-07-10 thomas
8807 795b88cb 2023-07-10 thomas argc -= optind;
8808 795b88cb 2023-07-10 thomas argv += optind;
8809 795b88cb 2023-07-10 thomas
8810 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
8811 795b88cb 2023-07-10 thomas if (cwd == NULL) {
8812 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
8813 795b88cb 2023-07-10 thomas goto done;
8814 795b88cb 2023-07-10 thomas }
8815 795b88cb 2023-07-10 thomas
8816 795b88cb 2023-07-10 thomas error = got_repo_pack_fds_open(&pack_fds);
8817 795b88cb 2023-07-10 thomas if (error != NULL)
8818 795b88cb 2023-07-10 thomas goto done;
8819 795b88cb 2023-07-10 thomas
8820 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8821 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
8822 795b88cb 2023-07-10 thomas if (error && error->code != GOT_ERR_NOT_WORKTREE)
8823 795b88cb 2023-07-10 thomas goto done;
8824 795b88cb 2023-07-10 thomas if (worktree) {
8825 795b88cb 2023-07-10 thomas repo_path = strdup(
8826 795b88cb 2023-07-10 thomas got_worktree_get_repo_path(worktree));
8827 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8828 795b88cb 2023-07-10 thomas error = got_error_from_errno("strdup");
8829 795b88cb 2023-07-10 thomas goto done;
8830 795b88cb 2023-07-10 thomas }
8831 795b88cb 2023-07-10 thomas
8832 795b88cb 2023-07-10 thomas /* Release work tree lock. */
8833 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8834 795b88cb 2023-07-10 thomas worktree = NULL;
8835 795b88cb 2023-07-10 thomas }
8836 795b88cb 2023-07-10 thomas }
8837 795b88cb 2023-07-10 thomas
8838 795b88cb 2023-07-10 thomas if (repo_path == NULL) {
8839 795b88cb 2023-07-10 thomas repo_path = strdup(cwd);
8840 795b88cb 2023-07-10 thomas if (repo_path == NULL)
8841 795b88cb 2023-07-10 thomas return got_error_from_errno("strdup");
8842 795b88cb 2023-07-10 thomas }
8843 795b88cb 2023-07-10 thomas
8844 795b88cb 2023-07-10 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8845 795b88cb 2023-07-10 thomas free(repo_path);
8846 795b88cb 2023-07-10 thomas if (error != NULL)
8847 795b88cb 2023-07-10 thomas goto done;
8848 795b88cb 2023-07-10 thomas
8849 795b88cb 2023-07-10 thomas error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8850 795b88cb 2023-07-10 thomas if (error)
8851 795b88cb 2023-07-10 thomas goto done;
8852 795b88cb 2023-07-10 thomas
8853 795b88cb 2023-07-10 thomas error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, 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 if (commit_id_str == NULL)
8858 795b88cb 2023-07-10 thomas commit_id_str = GOT_REF_HEAD;
8859 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&commit_id, NULL,
8860 795b88cb 2023-07-10 thomas commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8861 795b88cb 2023-07-10 thomas if (error)
8862 795b88cb 2023-07-10 thomas goto done;
8863 795b88cb 2023-07-10 thomas
8864 795b88cb 2023-07-10 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
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 for (i = 0; i < argc; i++) {
8869 795b88cb 2023-07-10 thomas if (force_path) {
8870 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&id, repo, commit,
8871 795b88cb 2023-07-10 thomas argv[i]);
8872 795b88cb 2023-07-10 thomas if (error)
8873 795b88cb 2023-07-10 thomas break;
8874 795b88cb 2023-07-10 thomas } else {
8875 795b88cb 2023-07-10 thomas error = got_repo_match_object_id(&id, &label, argv[i],
8876 795b88cb 2023-07-10 thomas GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
8877 795b88cb 2023-07-10 thomas repo);
8878 795b88cb 2023-07-10 thomas if (error) {
8879 795b88cb 2023-07-10 thomas if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8880 795b88cb 2023-07-10 thomas error->code != GOT_ERR_NOT_REF)
8881 795b88cb 2023-07-10 thomas break;
8882 795b88cb 2023-07-10 thomas error = got_object_id_by_path(&id, repo,
8883 795b88cb 2023-07-10 thomas commit, argv[i]);
8884 795b88cb 2023-07-10 thomas if (error)
8885 795b88cb 2023-07-10 thomas break;
8886 795b88cb 2023-07-10 thomas }
8887 795b88cb 2023-07-10 thomas }
8888 795b88cb 2023-07-10 thomas
8889 795b88cb 2023-07-10 thomas error = got_object_get_type(&obj_type, repo, id);
8890 795b88cb 2023-07-10 thomas if (error)
8891 795b88cb 2023-07-10 thomas break;
8892 795b88cb 2023-07-10 thomas
8893 795b88cb 2023-07-10 thomas switch (obj_type) {
8894 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_BLOB:
8895 795b88cb 2023-07-10 thomas error = cat_blob(id, repo, stdout);
8896 795b88cb 2023-07-10 thomas break;
8897 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TREE:
8898 795b88cb 2023-07-10 thomas error = cat_tree(id, repo, stdout);
8899 795b88cb 2023-07-10 thomas break;
8900 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_COMMIT:
8901 795b88cb 2023-07-10 thomas error = cat_commit(id, repo, stdout);
8902 795b88cb 2023-07-10 thomas break;
8903 795b88cb 2023-07-10 thomas case GOT_OBJ_TYPE_TAG:
8904 795b88cb 2023-07-10 thomas error = cat_tag(id, repo, stdout);
8905 795b88cb 2023-07-10 thomas break;
8906 795b88cb 2023-07-10 thomas default:
8907 795b88cb 2023-07-10 thomas error = got_error(GOT_ERR_OBJ_TYPE);
8908 795b88cb 2023-07-10 thomas break;
8909 795b88cb 2023-07-10 thomas }
8910 795b88cb 2023-07-10 thomas if (error)
8911 795b88cb 2023-07-10 thomas break;
8912 795b88cb 2023-07-10 thomas free(label);
8913 795b88cb 2023-07-10 thomas label = NULL;
8914 795b88cb 2023-07-10 thomas free(id);
8915 795b88cb 2023-07-10 thomas id = NULL;
8916 795b88cb 2023-07-10 thomas }
8917 795b88cb 2023-07-10 thomas done:
8918 795b88cb 2023-07-10 thomas free(label);
8919 795b88cb 2023-07-10 thomas free(id);
8920 795b88cb 2023-07-10 thomas free(commit_id);
8921 795b88cb 2023-07-10 thomas if (commit)
8922 795b88cb 2023-07-10 thomas got_object_commit_close(commit);
8923 795b88cb 2023-07-10 thomas if (worktree)
8924 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
8925 795b88cb 2023-07-10 thomas if (repo) {
8926 795b88cb 2023-07-10 thomas const struct got_error *close_err = got_repo_close(repo);
8927 795b88cb 2023-07-10 thomas if (error == NULL)
8928 795b88cb 2023-07-10 thomas error = close_err;
8929 795b88cb 2023-07-10 thomas }
8930 795b88cb 2023-07-10 thomas if (pack_fds) {
8931 795b88cb 2023-07-10 thomas const struct got_error *pack_err =
8932 795b88cb 2023-07-10 thomas got_repo_pack_fds_close(pack_fds);
8933 795b88cb 2023-07-10 thomas if (error == NULL)
8934 795b88cb 2023-07-10 thomas error = pack_err;
8935 795b88cb 2023-07-10 thomas }
8936 795b88cb 2023-07-10 thomas
8937 795b88cb 2023-07-10 thomas got_ref_list_free(&refs);
8938 795b88cb 2023-07-10 thomas return error;
8939 795b88cb 2023-07-10 thomas }
8940 795b88cb 2023-07-10 thomas
8941 795b88cb 2023-07-10 thomas __dead static void
8942 795b88cb 2023-07-10 thomas usage_info(void)
8943 795b88cb 2023-07-10 thomas {
8944 795b88cb 2023-07-10 thomas fprintf(stderr, "usage: %s info [path ...]\n",
8945 795b88cb 2023-07-10 thomas getprogname());
8946 795b88cb 2023-07-10 thomas exit(1);
8947 795b88cb 2023-07-10 thomas }
8948 795b88cb 2023-07-10 thomas
8949 795b88cb 2023-07-10 thomas static const struct got_error *
8950 795b88cb 2023-07-10 thomas print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
8951 795b88cb 2023-07-10 thomas struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8952 795b88cb 2023-07-10 thomas struct got_object_id *commit_id)
8953 795b88cb 2023-07-10 thomas {
8954 795b88cb 2023-07-10 thomas const struct got_error *err = NULL;
8955 795b88cb 2023-07-10 thomas char *id_str = NULL;
8956 795b88cb 2023-07-10 thomas char datebuf[128];
8957 795b88cb 2023-07-10 thomas struct tm mytm, *tm;
8958 795b88cb 2023-07-10 thomas struct got_pathlist_head *paths = arg;
8959 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
8960 795b88cb 2023-07-10 thomas
8961 795b88cb 2023-07-10 thomas /*
8962 795b88cb 2023-07-10 thomas * Clear error indication from any of the path arguments which
8963 795b88cb 2023-07-10 thomas * would cause this file index entry to be displayed.
8964 795b88cb 2023-07-10 thomas */
8965 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, paths, entry) {
8966 795b88cb 2023-07-10 thomas if (got_path_cmp(path, pe->path, strlen(path),
8967 795b88cb 2023-07-10 thomas pe->path_len) == 0 ||
8968 795b88cb 2023-07-10 thomas got_path_is_child(path, pe->path, pe->path_len))
8969 795b88cb 2023-07-10 thomas pe->data = NULL; /* no error */
8970 795b88cb 2023-07-10 thomas }
8971 795b88cb 2023-07-10 thomas
8972 795b88cb 2023-07-10 thomas printf(GOT_COMMIT_SEP_STR);
8973 795b88cb 2023-07-10 thomas if (S_ISLNK(mode))
8974 795b88cb 2023-07-10 thomas printf("symlink: %s\n", path);
8975 795b88cb 2023-07-10 thomas else if (S_ISREG(mode)) {
8976 795b88cb 2023-07-10 thomas printf("file: %s\n", path);
8977 795b88cb 2023-07-10 thomas printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
8978 795b88cb 2023-07-10 thomas } else if (S_ISDIR(mode))
8979 795b88cb 2023-07-10 thomas printf("directory: %s\n", path);
8980 795b88cb 2023-07-10 thomas else
8981 795b88cb 2023-07-10 thomas printf("something: %s\n", path);
8982 795b88cb 2023-07-10 thomas
8983 795b88cb 2023-07-10 thomas tm = localtime_r(&mtime, &mytm);
8984 795b88cb 2023-07-10 thomas if (tm == NULL)
8985 795b88cb 2023-07-10 thomas return NULL;
8986 795b88cb 2023-07-10 thomas if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
8987 795b88cb 2023-07-10 thomas return got_error(GOT_ERR_NO_SPACE);
8988 795b88cb 2023-07-10 thomas printf("timestamp: %s\n", datebuf);
8989 795b88cb 2023-07-10 thomas
8990 795b88cb 2023-07-10 thomas if (blob_id) {
8991 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, blob_id);
8992 795b88cb 2023-07-10 thomas if (err)
8993 795b88cb 2023-07-10 thomas return err;
8994 795b88cb 2023-07-10 thomas printf("based on blob: %s\n", id_str);
8995 795b88cb 2023-07-10 thomas free(id_str);
8996 795b88cb 2023-07-10 thomas }
8997 795b88cb 2023-07-10 thomas
8998 795b88cb 2023-07-10 thomas if (staged_blob_id) {
8999 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, staged_blob_id);
9000 795b88cb 2023-07-10 thomas if (err)
9001 795b88cb 2023-07-10 thomas return err;
9002 795b88cb 2023-07-10 thomas printf("based on staged blob: %s\n", id_str);
9003 795b88cb 2023-07-10 thomas free(id_str);
9004 795b88cb 2023-07-10 thomas }
9005 795b88cb 2023-07-10 thomas
9006 795b88cb 2023-07-10 thomas if (commit_id) {
9007 795b88cb 2023-07-10 thomas err = got_object_id_str(&id_str, commit_id);
9008 795b88cb 2023-07-10 thomas if (err)
9009 795b88cb 2023-07-10 thomas return err;
9010 795b88cb 2023-07-10 thomas printf("based on commit: %s\n", id_str);
9011 795b88cb 2023-07-10 thomas free(id_str);
9012 795b88cb 2023-07-10 thomas }
9013 795b88cb 2023-07-10 thomas
9014 795b88cb 2023-07-10 thomas return NULL;
9015 795b88cb 2023-07-10 thomas }
9016 795b88cb 2023-07-10 thomas
9017 795b88cb 2023-07-10 thomas static const struct got_error *
9018 795b88cb 2023-07-10 thomas cmd_info(int argc, char *argv[])
9019 795b88cb 2023-07-10 thomas {
9020 795b88cb 2023-07-10 thomas const struct got_error *error = NULL;
9021 795b88cb 2023-07-10 thomas struct got_worktree *worktree = NULL;
9022 795b88cb 2023-07-10 thomas char *cwd = NULL, *id_str = NULL;
9023 795b88cb 2023-07-10 thomas struct got_pathlist_head paths;
9024 795b88cb 2023-07-10 thomas char *uuidstr = NULL;
9025 795b88cb 2023-07-10 thomas int ch, show_files = 0;
9026 795b88cb 2023-07-10 thomas
9027 795b88cb 2023-07-10 thomas TAILQ_INIT(&paths);
9028 795b88cb 2023-07-10 thomas
9029 795b88cb 2023-07-10 thomas #ifndef PROFILE
9030 795b88cb 2023-07-10 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9031 795b88cb 2023-07-10 thomas NULL) == -1)
9032 795b88cb 2023-07-10 thomas err(1, "pledge");
9033 795b88cb 2023-07-10 thomas #endif
9034 795b88cb 2023-07-10 thomas
9035 795b88cb 2023-07-10 thomas while ((ch = getopt(argc, argv, "")) != -1) {
9036 795b88cb 2023-07-10 thomas switch (ch) {
9037 795b88cb 2023-07-10 thomas default:
9038 795b88cb 2023-07-10 thomas usage_info();
9039 795b88cb 2023-07-10 thomas /* NOTREACHED */
9040 795b88cb 2023-07-10 thomas }
9041 795b88cb 2023-07-10 thomas }
9042 795b88cb 2023-07-10 thomas
9043 795b88cb 2023-07-10 thomas argc -= optind;
9044 795b88cb 2023-07-10 thomas argv += optind;
9045 795b88cb 2023-07-10 thomas
9046 795b88cb 2023-07-10 thomas cwd = getcwd(NULL, 0);
9047 795b88cb 2023-07-10 thomas if (cwd == NULL) {
9048 795b88cb 2023-07-10 thomas error = got_error_from_errno("getcwd");
9049 795b88cb 2023-07-10 thomas goto done;
9050 795b88cb 2023-07-10 thomas }
9051 795b88cb 2023-07-10 thomas
9052 ad10f64e 2023-07-19 thomas error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_CVG_DIR);
9053 795b88cb 2023-07-10 thomas if (error) {
9054 795b88cb 2023-07-10 thomas if (error->code == GOT_ERR_NOT_WORKTREE)
9055 795b88cb 2023-07-10 thomas error = wrap_not_worktree_error(error, "info", cwd);
9056 795b88cb 2023-07-10 thomas goto done;
9057 795b88cb 2023-07-10 thomas }
9058 795b88cb 2023-07-10 thomas
9059 795b88cb 2023-07-10 thomas #ifndef PROFILE
9060 795b88cb 2023-07-10 thomas /* Remove "wpath cpath proc exec sendfd" promises. */
9061 795b88cb 2023-07-10 thomas if (pledge("stdio rpath flock unveil", NULL) == -1)
9062 795b88cb 2023-07-10 thomas err(1, "pledge");
9063 795b88cb 2023-07-10 thomas #endif
9064 795b88cb 2023-07-10 thomas error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9065 795b88cb 2023-07-10 thomas if (error)
9066 795b88cb 2023-07-10 thomas goto done;
9067 795b88cb 2023-07-10 thomas
9068 795b88cb 2023-07-10 thomas if (argc >= 1) {
9069 795b88cb 2023-07-10 thomas error = get_worktree_paths_from_argv(&paths, argc, argv,
9070 795b88cb 2023-07-10 thomas worktree);
9071 795b88cb 2023-07-10 thomas if (error)
9072 795b88cb 2023-07-10 thomas goto done;
9073 795b88cb 2023-07-10 thomas show_files = 1;
9074 795b88cb 2023-07-10 thomas }
9075 795b88cb 2023-07-10 thomas
9076 795b88cb 2023-07-10 thomas error = got_object_id_str(&id_str,
9077 795b88cb 2023-07-10 thomas got_worktree_get_base_commit_id(worktree));
9078 795b88cb 2023-07-10 thomas if (error)
9079 795b88cb 2023-07-10 thomas goto done;
9080 795b88cb 2023-07-10 thomas
9081 795b88cb 2023-07-10 thomas error = got_worktree_get_uuid(&uuidstr, 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 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9086 795b88cb 2023-07-10 thomas printf("work tree base commit: %s\n", id_str);
9087 795b88cb 2023-07-10 thomas printf("work tree path prefix: %s\n",
9088 795b88cb 2023-07-10 thomas got_worktree_get_path_prefix(worktree));
9089 795b88cb 2023-07-10 thomas printf("work tree branch reference: %s\n",
9090 795b88cb 2023-07-10 thomas got_worktree_get_head_ref_name(worktree));
9091 795b88cb 2023-07-10 thomas printf("work tree UUID: %s\n", uuidstr);
9092 795b88cb 2023-07-10 thomas printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9093 795b88cb 2023-07-10 thomas
9094 795b88cb 2023-07-10 thomas if (show_files) {
9095 795b88cb 2023-07-10 thomas struct got_pathlist_entry *pe;
9096 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
9097 795b88cb 2023-07-10 thomas if (pe->path_len == 0)
9098 795b88cb 2023-07-10 thomas continue;
9099 795b88cb 2023-07-10 thomas /*
9100 795b88cb 2023-07-10 thomas * Assume this path will fail. This will be corrected
9101 795b88cb 2023-07-10 thomas * in print_path_info() in case the path does suceeed.
9102 795b88cb 2023-07-10 thomas */
9103 795b88cb 2023-07-10 thomas pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
9104 795b88cb 2023-07-10 thomas }
9105 795b88cb 2023-07-10 thomas error = got_worktree_path_info(worktree, &paths,
9106 795b88cb 2023-07-10 thomas print_path_info, &paths, check_cancelled, NULL);
9107 795b88cb 2023-07-10 thomas if (error)
9108 795b88cb 2023-07-10 thomas goto done;
9109 795b88cb 2023-07-10 thomas TAILQ_FOREACH(pe, &paths, entry) {
9110 795b88cb 2023-07-10 thomas if (pe->data != NULL) {
9111 795b88cb 2023-07-10 thomas const struct got_error *perr;
9112 795b88cb 2023-07-10 thomas
9113 795b88cb 2023-07-10 thomas perr = pe->data;
9114 795b88cb 2023-07-10 thomas error = got_error_fmt(perr->code, "%s",
9115 795b88cb 2023-07-10 thomas pe->path);
9116 795b88cb 2023-07-10 thomas break;
9117 795b88cb 2023-07-10 thomas }
9118 795b88cb 2023-07-10 thomas }
9119 795b88cb 2023-07-10 thomas }
9120 795b88cb 2023-07-10 thomas done:
9121 795b88cb 2023-07-10 thomas if (worktree)
9122 795b88cb 2023-07-10 thomas got_worktree_close(worktree);
9123 795b88cb 2023-07-10 thomas got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9124 795b88cb 2023-07-10 thomas free(cwd);
9125 795b88cb 2023-07-10 thomas free(id_str);
9126 795b88cb 2023-07-10 thomas free(uuidstr);
9127 795b88cb 2023-07-10 thomas return error;
9128 795b88cb 2023-07-10 thomas }